Java Math - ceil() Method
The java.lang.Math.ceil() method returns the next highest integer value by rounding up the specified number, if necessary. In other words, it rounds the fraction UP of the given number. In special cases it returns the following:
- If the argument value is already an integer, then the result is the same as the argument.
- If the argument is NaN or 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: The value of Math.ceil(x) is exactly the value of -Math.floor(-x).
Syntax
public static double ceil(double arg)
Parameters
arg |
Specify a number. |
Return Value
Returns the next highest integer value by rounding UP the specified number, if necessary.
Exception
NA.
Example:
In the example below, ceil() method is used to round the fraction UP of the specified numbers.
import java.lang.*; public class MyClass { public static void main(String[] args) { System.out.println(Math.ceil(10.5)); System.out.println(Math.ceil(-10.5)); System.out.println(Math.ceil(0.5)); System.out.println(Math.ceil(-0.5)); } }
The output of the above code will be:
11.0 -10.0 1.0 -0.0
❮ Java.lang - Math