Java Tutorial Java Advanced Java Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

Java - Math ceil() Method



The Java 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 number.

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 Math Methods

5