ceil() Method in Java

Rumman Ansari   Software Engineer   2019-03-30   7005 Share
☰ Table of Contents

Table of Content:


Description

This Java tutorial shows how to use the ceil(double a) method of Math class under java.lang package. This 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.

The method ceil gives the smallest integer that is greater than or equal to the argument.

Syntax

This method has the following variants ?

double ceil(double d)
double ceil(float f)

Parameters

Here is the detail of parameters ?

  • A double or float primitive data type.

Return Value

The ceil(double a) method simply returns 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. Consider the following cases:

  • 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.

This method returns the smallest integer that is greater than or equal to the argument. Returned as a double.

 

Example

 
public class CeilMethod {

   public static void main(String args[]) {
      double d = -120.675;
      float f = -60;

      System.out.println(Math.ceil(d));
      System.out.println(Math.ceil(f));

      System.out.println(Math.floor(d));
      System.out.println(Math.floor(f));
   }
}

Output

-120.0
-60.0
-121.0
-60.0
Press any key to continue . . .

Example

This java example source code demonstrates the use of ceil(double a) method of Math class. Basically we just get the ceiling value of the user input.

 
import static java.lang.System.*;

import java.util.Scanner;

/*
 * This example source code demonstrates the use of
 * ceil(double a) method of Math class
 * Get the ceiling value of the user input
 */

public class MathCeiling {

	public static void main(String[] args) {
		// ask for user input
		out.print("Enter a value:");
		Scanner scan = new Scanner(System.in);
		// use scanner to get user console input
		double value = scan.nextDouble();
		// get the ceiling of a value
		double ceilValue = Math.ceil(value);
		out.println("ceiling of "+value+" = "+ceilValue);
		// close the scanner object to avoid memory leak
		scan.close();

	}

}

Output

Running the ceil(double a) method example source code of Math class will give you the following output

Enter a value:15.1
ceiling of 15.1 = 16.0
Press any key to continue . . .

Find ceiling value of a number using Math.ceil

 
/*
  Find ceiling value of a number using Math.ceil
  This java example shows how to find a ceiling value of a number using ceil method
  of Java Math class. ceil method returns the smallest interger which is not
  less than the value.
*/

public class FindCeilingExample {

  public static void main(String[] args) {

    /*
     * To find a ceiling value, use
     * static double ceil(double d) method of Math class.
     *
     * It returns a smallest integer which is not less than the argument value.
     */

    //Returns the same value
    System.out.println(Math.ceil(10));

    //returns a smallest integer which is not less than 10.1, i.e. 11
    System.out.println(Math.ceil(10.1));

    //returns a smallest integer which is not less than 5.5, i.e. 6
    System.out.println(Math.ceil(5.5));

    //in this case it would be -20
    System.out.println(Math.ceil(-20));

    //it returns -42 not -43. (-42 is grater than 42.4 :) )
    System.out.println(Math.ceil(-42.4));

    //returns 0
    System.out.println(Math.ceil(0));

  }
}

Output

Running the ceil(double a) method example source code of Math class will give you the following output

10.0
11.0
6.0
-20.0
-42.0
0.0
Press any key to continue . . .