본문 바로가기
Java

[Java] Math.ceil() 올림 / Math.floor() 내림 /Math.round 반올림 / Math.pow() 지수 / Math.random 난수( 0 <= x < 1인 실수) / Math.max 최댓값 / Math.min 최솟값

by bkuk 2022. 10. 3.

Math.ceil - 올림

문법

Math.ceil( double a)

예제

System.out.println( Math.ceil(10.3));
// 11.0 출력
System.out.println( Math.ceil(10.5));
// 11.0 출력
System.out.println( Math.ceil(10.6));
// 11.0 출력

 

Math.floor - 내림

문법

Math.floor( double a) 

예제

System.out.println( Math.floor(10.3));
// 10.0 출력
System.out.println( Math.floor(10.5));
// 10.0 출력
System.out.println( Math.floor(10.6));
// 10.0 출력

 

Math.floor - 내림

문법

Math.round( double a)

예제

System.out.println( Math.round( 10.3));
// 10 출력
System.out.println( Math.round( 10.5));
// 11 출력
System.out.println( Math.round( 10.6));
// 11 출력

 

Math.pow - 지수

문법

Math.pow( double a, double b)

예제

		System.out.println(Math.pow(10.0, 2.0));
        // 100.0 출력
		System.out.println(Math.pow(10, 2));
        // 100.0 출력
		System.out.println(Math.pow(10.0, 3.0));
        // 1000.0 출력

 

Math.random - 난수

문법

Math.random(), 난수 범위: 0 <= x < 1 실수

예제

System.out.println( Math.random());
// 0 <= x < 1 실수

System.out.println((int)(Math.random() * 10));
// 0 <= x < 10인 정수((int)를 통한 강제 형변환)

System.out.println( (int)(((Math.random() * 45) + 1 )) );
// 1 <= x < 46인 정수

 

Math.max - 최댓값

문법

Math.max(double a, double b)

예제

System.out.println( Math.max(10.0, 20.0));
// 20.0 출력

 

Math.min - 최솟값

문법

Math.min(double a, double b)

예제

System.out.println( Math.min(10.0, 20.0));
// 10.0 출력

댓글