Java Math ceil() Method
Java Math Class
Java Math class provides several methods to perform several operations on math calculations like max(), min(), sin(), cos(), round(), ceil(), floor(), abs() etc.
The java.lang.Math class contains various methods for performing basic numeric operations
Here, in the page we will discuss about the math ceil() Method in java.
Java Math ceil() Method :
The ceil() method returns the smallest double value that is greater than or equal to the argument, is an integer, and is closest to negative infinity.
Special Points :
- 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.
Note :
- The value of Math.ceil(x) is exactly the value of -Math.floor(-x).
Syntax :
public static double ceil(double a)
Parameters :
a - a value.
Throws Exception :
It does throws any exceptions and error.
Return Value :
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.
Example 1 :
class Main{ public static void main(String args[]) { double a = 6.3; double b = 5.0 / 0; double c = 0.0; double d = -0.0; double e = -0.15; System.out.println(Math.ceil(a)); // Input Infinity, Output Infinity System.out.println(Math.ceil(b)); // Input Positive Zero, Output Positive Zero System.out.println(Math.ceil(c)); // Input Negative Zero, Output Negative Zero System.out.println(Math.ceil(d)); // Input less than zero but greater than -1.0 // Output Negative zero System.out.println(Math.ceil(e)); } }
Output :
5.2535878724929015 Infinity -Infinity 0.0 -0.0
Explanation :
In the preceding example, the java math ceiling() method is used to demonstrate how it works. This method returns the double value that is greater than or equal to the argument and is equal to the nearest mathematical integer.
Example 2 :
public class Main { public static void main(String[] args) { //declaring variables double x = 50.0; double y = 75.0; double z = 55.0; double e = 0.0; //inpitting a positive value //outputing a positive value System.out.println("Math.ceil(" + x + ") = " + Math.ceil(x)); System.out.println("Math.ceil(" + y + ") = " + Math.ceil(y)); System.out.println("Math.ceil(" + z + ") = " + Math.ceil(z)); System.out.println("Math.ceil(" + e + ") = " + Math.ceil(e)); } }
Output :
Math.ceil(50.0) = 50.0 Math.ceil(75.0) = 75.0 Math.ceil(55.0) = 55.0 Math.ceil(0.0) = 0.0
Explanation :
In the preceding example, we take the simple example how it work with positive value when we passed through it .
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment