In Java, Math.ceil(a) returns the smallest whole number that is greater than or equal toa. In plain words: it rounds up. Math.ceil(4.1) is 5.0, and Math.ceil(4.0) stays 4.0.
"Up" means toward positive infinity, so negative numbers move toward zero: Math.ceil(-2.7) is -2.0. Use it for "how many containers do I need" questions: pages, boxes, buses, billed minutes.
A static method of the java.lang.Math class, available since the early versions of Java (JDK 1.0). Call it on the class name, as in Math.ceil(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass a double. The result is a double holding a whole number, so cast it if you need an int.
Java
double a = Math.ceil(4.1); // 5.0
double b = Math.ceil(-2.7); // -2.0
int pages = (int) Math.ceil(23 / 10.0); // 3
int wrong = (int) Math.ceil(23 / 10); // 2 (int division first!)
Call
What it does
Math.ceil(7.01)
Returns 8.0.
Math.ceil(7.0)
Already whole: returns 7.0.
(int) Math.ceil((double) items / perPage)
Number of pages or boxes needed.
Math.ceil(-0.5)
Returns -0.0 (a negative zero).
For whole-number division you can avoid doubles completely: (items + perPage - 1) / perPage works for positive ints, and Java 18 adds Math.ceilDiv.
Library declarations
Java
// java.lang.Math
public static double ceil(double a)
Parameters
Parameter
Description
a
The value to round up. int, long, and float arguments are widened to double.
Return Value
Return Value
Description
double
The smallest whole-number double that is >= a. NaN, infinities, zeros, and values that are already whole are returned unchanged.
Up means toward positive infinity. -2.0 is greater than -2.7, so rounding up moves a negative number closer to zero.
A negative zero. -0.5 rounds up to zero, but the result keeps the minus sign of the input, so it prints as -0.0.
Unchanged values. 4.0 is already whole, every double as large as 1e300 is whole, and NaN stays NaN; Java prints the big value in E notation as 1.0E300.
Frequently Asked Questions
It rounds a number up to the nearest whole number and returns it as a double. Math.ceil(4.1) returns 5.0.
Because many doubles are far too large for an int or long. Cast the result yourself, for example (int) Math.ceil(x), when you know it fits.
Because 23 / 10 is integer division, which already gives 2 before ceil runs. Write Math.ceil(23 / 10.0) or cast one operand to double.
It moves toward zero, because zero is the bigger direction. Math.ceil(-2.7) is -2.0, and Math.ceil(-0.5) is -0.0.
ceil always rounds up, so 4.1 becomes 5.0. round goes to the nearest whole number, so 4.1 becomes 4, and it returns a long instead of a double.