Learn and Learn

Learn And Learn - great place for tutorials, references and how-to

Java – Find Smallest Double Value – ceil() Method with Examples

java.lang.Math.ceil() method is used to find the smallest double value in Java, (closest to negative infinity), that is greater than or equal to the argument and is equal to a mathematical integer of a double value for the given input (x – parameter).
Note that the value of Math.ceil(x) is exactly the value of -Math.floor(-x).

 

ceil() method exists in Math class of java.lang package.
Since: JDK1.0

 

Declaration of ceil() Method

The declaration of ceil() method is:

 

Syntax of ceil() Method

The syntax of ceil() method in Java is:

Example

Output:

80.0

Example
By importing java.lang package the syntax of ceil() method can be used as:

Output:

-68.0

 

Special Cases of ceil() Method in Java

If the argument value is already equal to a mathematical integer, then the result is the same as the argument.
If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.
If the argument value is less than zero but greater than -1.0, then the result is negative zero.
Example

Output:

70.0
0.0
0.0
Infinity
-Infinity
NaN
-0.0

 

Parameters of ceil() Method in Java

x – Where x is any valid double input value. This parameter is required.

 

Error Handling

If the x parameter is not a number ceil() method returns an error.
If there is no argument (x – input value) passes to the method, the compiler will produce an error.Example

Output:
Error

 

Return Value of ceil() Method in Java

ceil() method 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 of a double value for the given input (x – parameter).the smallest (closest to negative infinity) floating-point value that is greater than or equal to the argument and is equal to a mathematical integer.

 

Java ceil() Method Example 1


Output:

smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer of a Negative Value: -18.0
smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer of a Positive Value: 28.0
smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer of Zero: 0.0

 

Java ceil() Method Example 2


Output:

1.7976931348623157E308
1.0
NaN
-1022.0
1.0
16.0
0.0
-2.0
-7.88569E8

 

 

LearnAndLearn.com © 2018 - All rights reserved