자바(Java)
자바 Math.ceil 사용 방법
xemaker
2020. 9. 1. 13:56
자바 Math.ceil 을 그냥 막 쓰면 정수가 나온다. 때문에 아래와 같이 double로 형변환을 해야 소수점이 발생하여 정상적으로 ceil 을 할 수 있다.
System.out.println((double) 12000 / 5000);
int n = (int) Math.ceil((double) 12000 / 5000);
System.out.println(n);
결과
2.4
3
즉, 12000을 5000 으로 나누면 2.4가 나오고 ceil 을 하면 3이 된다.
자바 api 설명
double java.lang.Math.ceil(double a)
- ceilpublic static double ceil(double a)
Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer. Special cases:
- If the argument value is already equal to a mathematical integer, then the result is the same as the argument.
- If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.
- If the argument value is less than zero but greater than -1.0, then the result is negative zero.
Note that the value of Math.ceil(x) is exactly the value of -Math.floor(-x).
Parameters:a - a value.Returns:the smallest (closest to negative infinity) floating-point value that is greater than or equal to the argument and is equal to a mathematical integer.