Java Math ceil() Method

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)

Example 1 :  

Run

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

Example 2 :  

Run

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 

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription