Math.ceil() in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

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.

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.

math ceil in java

More Examples of Math.ceil()

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.