Java Math.ceil() Method with Examples

Finding the ceiling value(rounded up value) for some given number (Java math ceil method)

Now let’s move on to another method, which is ceil. This method returns the smallest double number(equal to an integer value) greater than or equal to the given double value. This is like if you have the number 17.34, the ceiling value of this value is 18.0, so it’s rounding up the value. Have a look at the below program –

import java.lang.Math;
public class UnderstandingMath {
public static void main(String[] args) {
System.out.println(Math.ceil(17.34));
}
}

The output of the above program comes out to be 18.0, as desired. Even if the value is 17.01, the ceiling value is going to be 18.0, so it is always rounding up.

Special cases –

  • If the argument is NaN, or infinity or zero(positive or negative), then the output is also the same as the argument.
  • If the argument is equal to the mathematical integer, then the output is the same as the argument.