Math Class in Java
➤ Math Class Methods
➤ Import Math Class
➤ Math.PI in Java
➤ Math.sqrt() in Java
➤ Math.pow() in Java
➤ Math.max() in Java
➤ Math.abs() in Java
➤ Math.ceil() in Java
➤ Math.floor() in Java
➤ Math.round() in Java
➤ Math.random() Java
Java.lang.Object Class
➤ Object Class in Java
➤ getClass() method
➤ hashCode() Method
➤ toString() Method
➤ Java equals() Method
➤ == vs equals() in Java
➤ HashCode() vs equals()
➤ Clone() Method in Java
➤ Object Class Quiz Set-1
➤ Object Class Quiz Set-2
➤ equals() Method Quiz
➤ Hashcode Value Quiz
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 the 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 the 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 the Math class statically and then we can invoke ceil() method without calling through its class name.
import static java.lang.Math.*;
public 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;
public 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 an 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 do you find anything incorrect? Let us know in the comments. Thank you!