Math.ceil() in Java

The ceil() is a function of the Math class present in java.lang package. Math.ceil() rounds up a double or float number to the nearest Mathematical integer greater than or equal to the given floating-point number. So basically, it rounds up a number upwards in the number line.
Syntax of Math.ceil()
The signature of ceil() is defined as :
And the syntax of the ceil() method is :
Note: Since ceil() is a static method, therefore we don't need to create an object, we can directly call the function using the class name(Math) and dot(.) operator.
Parameters of Math.ceil()
The ceil function takes a single parameter, the value to be rounded off upward.
- value: It is of type double
If a variable of data type other than double is passed to the Math.ceil() function, the compiler will implicitly typecast the variable to double. If implicit typecasting is not possible on that variable, a runtime TypeMismatch error will be thrown.
Return Values of Math.ceil()
Return Type: double
The ceil function returns a double value, which is equal to the nearest Mathematical integer greater than or equal to the passed parameter.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Exceptions of Math.ceil()
If the parameter's data type is correct, the ceil() function does not throw any exceptions. When the data type cannot be implicitly converted to double, it will throw a compile-time error.
Output
Example of Math.ceil()
Output:
What is Math.ceil() in Java?
Math.ceil() converts the number to the nearest integer greater than or equal to the given number; if an integer is passed as an argument, the result of Math.ceil() will be the same integer, but since Math.ceil() returns a double value the integer will be typecasted to double.

More Examples of Math.ceil()
Turn Learning into Career Growth
Example 1
Let's see an example to calculate ceil of a number and then typecast it to an integer.
Output:
Explanation: The value of 5/3 will be 1.6666667, and when we typecast this to int, it will become 1. However, if we use Math.ceil() before typecasting Math.ceil(), it will convert 1.666667 to 2.0, and then 2.0 is typecasted to 2.
Example 2
If the input parameter is a character, the ceil() function will return the ceil of the ASCII value of that character, which will be the ASCII value.
Note: A compile-time error will be thrown if String is passed as a parameter. incompatible types: String cannot be converted to double.
Output:
Example 3
If the input parameter is infinity, ceil() will return infinity with the same sign.
Output:
Example 4
If the input parameter is a zero, Ceil() function will return zero with the same sign as the input.
Output:
Example 5
If the input parameter is less than 0 but greater than -1, ceil function will return -0.0 as output.
Output:
Conclusion
- Math.ceil() take a double value as a parameter.
- Math.ceil() returns a double value that is equal to the nearest mathematical integer greater or equal to the given number.
- If the input parameter is a character ceil function returns the ceil of the ASCII value of the character.
- If the input parameter is infinity, the ceil function returns infinity with the same sign as the input.
- If the input parameter is zero, the ceil function returns zero with the same sign as the input parameter.
- If the input parameter is less than 0 but greater than -1, the ceil function returns -0.0.