.ceil()

The Math.ceil() method returns the double value that is a mathematical integer and is greater than or equal to the original value.

Syntax

Math.ceil(num)
  • The result is the same as the argument if the argument value is already equal to a mathematical integer.
  • The result is identical to the argument if the argument is NaN, +/- infinity, or +/- zero.
  • The parameter num is of type double.
  • The result is of type double

Example 1

The following example demonstrates using .ceil():

// Example.java
public class Example {
public static void main(String args[]) {
Double value1 = 8.3;
Double value2 = 8.9;
System.out.println(Math.ceil(value1));
System.out.println(Math.ceil(value2));
}
}

This results in the following output:

9.0
9.0

Example 2

The following example demonstrates using .ceil() with negative numbers:

// Example.java
public class Example {
public static void main(String args[]) {
Double value1 = -0.5;
Double value2 = -1.3;
System.out.println(Math.ceil(value1));
System.out.println(Math.ceil(value2));
}
}

This results in the following output:

-0.0
-1.0

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Java on Codecademy

Contributors