Java Math ceil
ceil() accepts double value as an argument and returns the smallest integer which is greater than or equal to the argument. The returned value is of type double.
Following is the syntax of ceil() method.
double result = ceil(double value)
Since the definition of ceil() function has double datatype as argument, you can pass int, float or long as arguments; because these datatypes will implicitly promote to double.
Example 1 Mathceildouble
In the following example, we pass a double value as argument to ceil() method and find its ceiling value.
Java Program
public class MathExample { public static void main(String[] args) { double x = 3.14D; double a = Math.ceil(x); System.out.println(a); } }
Output
4.0
Example 2 Mathceilfloat
In the following example, we pass a float value as argument to ceil() method and find the ceiling integer of its value.
Java Program
public class MathExample { public static void main(String[] args) { float x = 2.365F; double a = Math.ceil(x); System.out.println(a); } }
Output
3.0
Example 3 Mathceilint
In the following example, we pass a int value as argument to ceil() method and find its ceiling value.
If the argument is already a mathematical integer value, the return value is same as that of the argument.
Java Program
public class MathExample { public static void main(String[] args) { int x = 3; double a = Math.ceil(x); System.out.println(a); } }
Output
3.0
Example 4 Mathceillong
In the following example, we pass a long value as argument to ceil() method and find its ceiling value.
Java Program
public class MathExample { public static void main(String[] args) { long x = 3999999999999L; double a = Math.ceil(x); System.out.println(a); } }
Output
3.999999999999E12
Example 5 Mathceil With Negative Number Argument
In the following example, we pass a negative value as argument to ceil() method and find its ceiling value.
Java Program
public class MathExample { public static void main(String[] args) { double x = -12.4; double a = Math.ceil(x); System.out.println(a); } }
Output
-12.0
Example 6 Mathceil Arguments of NaN, Negative Infinity, Positive Infinity
For the arguments of NaN, negative infinity or positive infinity; ceil() returns the same value as that of argument.
Java Program
public class MathExample { public static void main(String[] args) { System.out.println(Math.ceil(Double.NaN)); System.out.println(Math.ceil(Double.POSITIVE_INFINITY)); System.out.println(Math.ceil(Double.NEGATIVE_INFINITY)); } }
Output
NaN Infinity -Infinity
Conclusion
In this Java Tutorial, we learned about Java Math.ceil() function, with example programs.