Java ceil Function

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

Syntax

The basic syntax of the Math.ceil in Java Programming language is as shown below. The following 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, it will return the same argument.
  • It will return the same argument when the number argument is not a number (NaN) or Infinity.
  • If it is less than zero but greater than -1.0, it will return a negative zero as output.

Java math 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 Java program, we will find both positive and negative ceiling values and display the output.

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 Function directly on Positive double values. If you observe the code snippet below, though 20.15 is near 20, the ceiling method returns 21 as output. It is because 21 will be the smallest integer value that 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 method 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 math ceil on Array example

In this program, we find the Ceiling values of bulk data. Here, we will declare an array of double types and find the closest 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("%.2f\n", Math.ceil(CeilArray[i]));
 }
 }
}
11.00
-15.00
23.00
96.00
-4.00
13.00

In this example, we declared an Array of double types and assigned some random values.

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

Next, We used Java For Loop to iterate the Array. Within the For Loop, we initialized the i as 0. Next, the compiler will check for the condition (i < CeilArray.length). As long as the condition is the 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 math double method to find the corresponding closet (ceiling) values and print the output.

System.out.format("%.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 will 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(Math.ceil(x));
		}
	}
}
-1.0
-1.0
16.0
16.0
26.0
-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 to find the corresponding closet number.

System.out.println(Math.ceil(x));