In Java, Math.ceilDiv(x, y) divides x by y and rounds the result up, toward positive infinity, without ever leaving integer arithmetic. Math.ceilDiv(23, 10) is 3, while the plain 23 / 10 is 2.
It answers "how many containers do I need?": pages for a list, boxes for a shipment, buses for a trip. It replaces the old tricks (x + y - 1) / y and (int) Math.ceil((double) x / y), and it also gives the right answer for negative numbers.
A static method of the java.lang.Math class, added in Java 18 (JDK 18). Call it on the class name, as in Math.ceilDiv(...). No import is needed, because classes in java.lang are always visible. Requires Java 18 or newer.
Syntax
Pass the dividend first, then the divisor. Both can be int, or the dividend can be a long.
Java
int pages = Math.ceilDiv(23, 10); // 3
int boxes = Math.ceilDiv(20, 10); // 2 (exact fit)
int down = Math.ceilDiv(-7, 2); // -3
long big = Math.ceilDiv(10_000_000_000L, 3); // 3333333334
Call
What it does
Math.ceilDiv(23, 10)
int overload: 2.3 rounded up gives 3.
Math.ceilDiv(-7, 2)
-3.5 rounded up (toward zero here) gives -3.
Math.ceilDiv(10_000_000_000L, 3)
long dividend, int divisor: returns a long.
Math.ceilDiv(5, 0)
Throws ArithmeticException with the message / by zero.
Java 18 added three overloads. With a long dividend the result is always a long, because the quotient can be as large as the dividend itself.
Library declarations
Java
// java.lang.Math (Java 18+)
public static int ceilDiv(int x, int y)
public static long ceilDiv(long x, int y)
public static long ceilDiv(long x, long y)
Parameters
Parameter
Description
x
The dividend: the number being divided (an int or a long).
y
The divisor: the number to divide by (an int or a long). Must not be zero.
Return Value
Return Value
Description
int / long
The smallest integer that is greater than or equal to the exact quotient x / y. It is long for the two overloads that take a long dividend.
Exact quotient first. -7 / 2 is exactly -3.5. Rounding up gives -3 and rounding down gives -4; plain division truncates toward zero, so it agrees with ceilDiv here.
Positive results split the other way. For -7 / -2 and 7 / 2 the exact quotient is 3.5; ceilDiv goes up to 4, while floorDiv and the plain operator both give 3.
A long dividend. 10000000000 / 3 is 3333333333.33..., so the long result rounds up to 3333333334; the divisor 3 is an int, which selects the (long, int) overload.
Example 3 — The / by zero Error and the MIN_VALUE Overflow
Error: / by zero
MIN_VALUE / -1: -2147483648
Zero items: 0
1 item, 1000 per box: 1
How It Works
Division by zero. The divisor 0 makes the underlying integer division fail, so an ArithmeticException is thrown and its message is printed by the catch block.
Silent overflow. The true answer for Integer.MIN_VALUE / -1 is +2147483648, one more than Integer.MAX_VALUE; it wraps back to -2147483648 and no exception is thrown.
Empty and tiny orders. Zero items need zero boxes, but even 1 item in a box of 1000 needs one whole box, because any positive fraction rounds up to 1.
Frequently Asked Questions
It divides two integers and rounds the quotient up toward positive infinity. Math.ceilDiv(23, 10) returns 3, while 23 / 10 returns 2.
Java 18. On older versions, write (x + y - 1) / y for positive numbers, or use (int) Math.ceil((double) x / y).
Use Math.ceilDiv(totalItems, itemsPerPage). With 23 items and 10 per page it returns 3 pages, and with exactly 20 items it returns 2.
ceilDiv rounds the exact quotient up and floorDiv rounds it down. They differ whenever the division is not exact: for 7 / 2 ceilDiv returns 4 and floorDiv returns 3.
Math.ceilDiv throws ArithmeticException with the message / by zero. The only silent overflow is Math.ceilDiv(Integer.MIN_VALUE, -1), which returns Integer.MIN_VALUE; ceilDivExact throws in that case.