Math.ceil() Method in Java

The ceil() method of java.lang.Math class 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.

The word CEIL is short for CEILING. The Math.ceil() method essentially rounds up to the nearest whole number and is the opposite of the Math.floor() method (which rounds down to the nearest whole number).

Method prototype of the java.lang.Math.ceil(),
public static double ceil(double a)

Java Math.ceil() Examples

public class Test{
   public static void main(String[] args) {
      System.out.println(Math.ceil(2.0)); // 2.0
      System.out.println(Math.ceil(-2.0)); // -2.0

      System.out.println(Math.ceil(2.5)); // 3.0
      System.out.println(Math.ceil(-2.5)); // -2.0

      System.out.println(Math.ceil(2.9)); // 3.0
      System.out.println(Math.ceil(-2.9)); // -2.0
   }
}

Output:-

2.0
-2.0
3.0
-2.0
3.0
-2.0

More Examples

When argument is positive

Math.ceil(2.0)2.0
Math.ceil(2.1)3.0
Math.ceil(2.5)3.0
Math.ceil(2.9)3.0
Math.ceil(3.0)3.0
Math.ceil(3.3)4.0
Math.ceil(3.5)4.0
Math.ceil(3.9)4.0

When argument is negative

Math.ceil(-2.0)-2.0
Math.ceil(-2.1)-2.0
Math.ceil(-2.5)-2.0
Math.ceil(-2.9)-2.0
Math.ceil(-3.0)-3.0
Math.ceil(-3.3)-3.0
Math.ceil(-3.5)-3.0
Math.ceil(-3.9)-3.0

If we import Math class statically and then we can invoke ceil() method without calling through it’s class name.

import static java.lang.Math.*;
class Test{
   public static void main(String[] args) {
      System.out.println(ceil(9.9)); // 10.0
      System.out.println(ceil(5.0)); // 5.0
   }
}

The “import static java.lang.Math.*;” statement will import all static members of the Math class. But if we want to import only the ceil() method of the Math class, not another static method and variables of Math class then we can use the “import static java.lang.Math.ceil;” statement. Learn more about static import in Java

import static java.lang.Math.ceil;
class Test{
   public static void main(String[] args) {
      System.out.println(ceil(9.9)); // 10.0
      System.out.println(ceil(5.0)); // 5.0
   }
}

Special cases

1) If passed values a integer then the same value is returned.

Math.ceil(5) => 5.0
Math.ceil(6.0) => 6.0

2) If the argument is zero (either positive or negative), infinity or NaN then also the same value is returned.

// argument is zero
Math.ceil(0) => 0.0
Math.ceil(-0) => 0.0

// argument is INFINITY
Math.ceil(Double.POSITIVE_INFINITY) => Infinity
Math.ceil(Double.NEGATIVE_INFINITY) => -Infinity

// argument is NaN
Math.ceil(Double.NaN) => NaN

3) If the argument value is less than zero but greater than -1.0, then the result is negative zero.

Math.ceil(-0.0)-0.0
Math.ceil(-0.1)-0.0
Math.ceil(-0.3)-0.0
Math.ceil(-0.5)-0.0
Math.ceil(-0.7)-0.0
Math.ceil(-0.9)-0.0
Math.ceil(-1.0)-1.0

Note:- the value of Math.ceil(x) is exactly the value of -Math.floor(-x).

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Also See,

Leave a Reply