java.lang.Math ceil(double a)

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.

Method Syntax :

public static double ceil(double a)

Parameter Input :

DataType Parameter Description
double a a value

Method Returns :

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.

Compatibility Version :

Requires Java 1.0 and up

Exception :

N/A

Java Code 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.

package com.javatutorialhq.java.examples;

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();

	}

}

Sample Output :

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

Enter a value:12.35
ceiling of 12.35 = 13.0

Exception Scenario :

N/A

Similar Method :

  • N/A

Suggested Reading List :

References :