Java Math ceil() Method
The ceil() method of Math class returns the smallest double value which is closest to negative infinity and is greater than or equal to the argument.
Syntax:
public static double ceil(double a)
Parameters:
The parameter ‘a’ represents a value.
Return Value:
The ceil() method returns the smallest floating-point value greater than or equal to argument and is equal to a mathematical integer.
- It returns same value as the argument, if the argument value is equal to a mathematical integer.
- It returns the same value as the argument, if we pass a NaN or infinity or positive zero or negative zero argument.
- It will return negative zero, if the argument value is less than zero but greater than -1.0.
Example 1:
public class JavaMathCeilExample1 { public static void main(String[] args) { double a=8.1; //returns the smallest value which is greater than or equal to argument System.out.println("Smallest closest double value of "+ a +" = "+Math.ceil(a)); } }
Output:
Smallest closest double value of 8.1 = 9.0
Example 2:
public class JavaMathCeilExample2 { public static void main(String[] args) { double a=-0.0d/0.0d; //return NaN value System.out.println("Smallest closest double value of "+ a +" = "+Math.ceil(a)); } }
Output:
Smallest closest double value of NaN = NaN
Example 3:
public class JavaMathCeilExample3 { public static void main(String[] args) { double a=-0.0; //returns a zero with the same sign as the argument ,if the argument is zero. System.out.println("Smallest closest double value of "+ a +" = "+Math.ceil(a)); } }
Output:
Smallest closest double value of -0.0 = -0.0
Example 4:
public class JavaMathCeilExample4 { public static void main(String[] args) { double a=-5/0.0; //returns infinity with the same sign as the argument ,if the argument is infinity. System.out.println("Smallest closest double value of "+ a +" = " +Math.ceil(a)); } }
Output:
Smallest closest double value of -Infinity = -Infinity