Java ceil Function

The Java ceil Function is one of the Java Math functions, which is to return the smallest integer value, which is greater than or equal to the specified expression or an individual number.

Java ceil Function syntax

The basic syntax of the Math.ceil in Java Programming language is as shown below. The following Java ceil function will accept positive or negative double value as an argument. It returns the smallest integer value, which is greater than or equal to the specified expression or Value and equal to a mathematical integer.

static double ceil(double number); //Return Type is Double

// In order to use in program: 
Math.ceil(double number);

Number: It can be a double value or a valid numerical expression on which you want to perform the ceiling.

  • If the number argument is a positive or negative number, the Java Math ceil function will return the ceiling value.
  • If the number argument is positive or negative zero, the Math.ceil function will return the same argument.
  • When the number argument is not a number (NaN) or Infinity, the Java Math ceil function will return the same argument.
  • If it is less than zero but greater than -1.0, the Math.ceil will return negative zero as output.

Java ceil Function Example

The Java Math.ceil Function finds the smallest integer value, which is greater than or equal to the numeric values. In this ceil Java program, We are going to find the ceiling values of both positive and negative values and display the output.

// Java Math.ceil Function
package MathFunctions;

public class CeilMethod {
 public static void main(String[] args) {
 double a = Math.ceil(10.90 + 15.10 - 22.50 + 1.50);
 System.out.println("Ceiling value = " + a); 

 System.out.println("\nCeiling value of Positive Number: " + Math.ceil(20.15));
 System.out.println("Ceiling value of Positive Number: " + Math.ceil(20.95));
 
 System.out.println("\nCeiling value of Negative Number: " + Math.ceil(-10.80));
 System.out.println("Ceiling value of Negative Number: " + Math.ceil(-10.05));
 }
}
Java ceil Function 1

First, we declared a variable of type Double and performed the Math.ceil function directly on expression.

double a = Math.ceil(10.90 + 15.10 - 22.50 + 1.50);
System.out.println("Ceiling value = " + a);	

Next, we used the Math.ceil Function directly on Positive double values. If you observe the below code snippet, though 20.15 is near to 20, the ceiling method is returning 21 as output. It is because 21 will be the smallest integer value which is greater than

System.out.println("\nCeiling value of Positive Number: " + Math.ceil(20.15));
System.out.println("Ceiling value of Positive Number: " + Math.ceil(20.95));

Next, we used the Math ceil Function directly on Negative double values.

System.out.println("\nCeiling value of Negative Number: " + Math.ceil(-10.80));
System.out.println("Ceiling value of Negative Number: " + Math.ceil(-10.05));

Java ceil on Array example

In this Java program, we find the Ceiling values of bulk data. Here, we are going to declare an array of double type and find the closest (ceiling) values of array elements.

package MathFunctions;

public class CeilMethodOnArray {
 public static void main(String[] args) {
 
 double [] CeilArray = {10.46, -15.98, 22.44, 95.9999, -4.8799, 12.8597};

 for (int i = 0; i < CeilArray.length; i++) {
 System.out.format("Ceiling value of Array Element = %.2f\n", Math.ceil(CeilArray[i]));
 }
 }
}
Ceiling value of Array Element = 11.00
Ceiling value of Array Element = -15.00
Ceiling value of Array Element = 23.00
Ceiling value of Array Element = 96.00
Ceiling value of Array Element = -4.00
Ceiling value of Array Element = 13.00

In this math ceil function example, we declared an Array of double type and assigned some random values.

double [] CeilArray = {10.46, -15.98, 22.44, 95.9999, -4.8799, 12.8597};

Next, We used the Java For Loop to iterate the Array. Within the For Loop, we initialized the i value as 0. Next, the compiler will check for the condition (i < CeilArray.length). As along the condition is True statement inside the for loop executed.

TIP: CeilArray.length finds the length of an array.

for (int i = 0; i < CeilArray.length; i++) {

Here, the compiler will call the Java Math.ceil method ( static double ceil(double number) ) to find the corresponding closet (ceiling) values and print the output.

System.out.format("Ceiling value of Array Element = %.2f\n", Math.ceil(CeilArray[i]));

NOTE: To find the closet (ceiling) value of a single item, then use: Math.ceil(myArray[index_position])

Java ceil function on Arraylist example

In this program, we are going to declare an arraylist of double type and find the closet (ceiling) values of list elements.

package MathFunctions;

import java.util.ArrayList;

public class CeilMethodOnArrayList {
	public static void main(String[] args) {
		
		ArrayList<Double> CeilList = new ArrayList<Double>(5);
		CeilList.add(-1.998);
		CeilList.add(-1.05);
		CeilList.add(15.9876);
		CeilList.add(15.01);
		CeilList.add(25.98);
		CeilList.add(-98.1289);
		
		for (double x : CeilList) {
			System.out.println("Ceiling value of ArrayList =  " + Math.ceil(x));
		}
	}
}
Ceiling value of ArrayList =  -1.0
Ceiling value of ArrayList =  -1.0
Ceiling value of ArrayList =  16.0
Ceiling value of ArrayList =  16.0
Ceiling value of ArrayList =  26.0
Ceiling value of ArrayList =  -98.0

We used the Java For Loop to iterate the double values in ArrayList

for (double x : CeilList) {

Here, we used the ceil Math function directly inside the System.out.format statement. Next, the compiler will call the ceiling method ( static double ceil(double x) ) to find the corresponding closet (ceil) values.

System.out.println("Ceiling value of ArrayList =  " + Math.ceil(x));