Java Math ceilDivExact() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 18+

Definition

In Java, Math.ceilDivExact(x, y) divides two whole numbers and rounds the quotient up, toward positive infinity. Math.ceilDivExact(23, 10) is 3, because 23 items need 3 boxes of 10.

It is the overflow-checked sibling of ceilDiv. Only one input can overflow, MIN_VALUE divided by -1; there ceilDivExact throws an ArithmeticException instead of returning a wrong number.

A static method of the java.lang.Math class, added in Java 18 (JDK 18). Call it on the class name, as in Math.ceilDivExact(...). No import is needed, because classes in java.lang are always visible. Requires Java 18 or newer.

Syntax

Pass the total first and the group size second, both int or both long. The result is the smallest whole number of groups that holds everything.

Java
int boxes = Math.ceilDivExact(23, 10);               // 3
long pages = Math.ceilDivExact(1_000_000_001L, 10L);   // 100000001
Math.ceilDivExact(5, 0);                               // throws ArithmeticException
CallWhat it does
Math.ceilDivExact(23, 10)int overload: 2.3 rounds up to 3.
Math.ceilDivExact(20, 10)Already exact: returns 2.
Math.ceilDivExact(-23, 10)Up means toward positive infinity: -2.3 becomes -2.
Math.ceilDivExact(Integer.MIN_VALUE, -1)Throws ArithmeticException ("integer overflow").

The classic trick (items + perBox - 1) / perBox gives the same answer for small positive numbers, but the addition can itself overflow for large ones. ceilDivExact has no such problem and also works for negative numbers.

Library declarations
Java
// java.lang.Math (Java 18+)
public static int ceilDivExact(int x, int y)
public static long ceilDivExact(long x, long y)

Parameters

ParameterDescription
xThe dividend (for example the number of items).
yThe divisor (for example the capacity of one box). Must not be zero.

Return Value

Return ValueDescription
int / longThe quotient rounded up: the smallest whole number that is greater than or equal to the exact quotient.
Throws ArithmeticException/ by zero if y is 0; integer overflow or long overflow for MIN_VALUE / -1.

Compare with Similar Methods

Rounding up integer division, compared:

MethodHow it differs
ceilDiv(x, y)Same rounding, but MIN_VALUE / -1 wraps instead of throwing.
floorDivExact(x, y)Rounds down instead of up; also overflow-checked.
divideExact(x, y)Rounds toward zero, which is neither up nor down for negatives.
Math.ceil(x / (double) y)Works through double; fine for ints, but long values above 2^53 lose precision.

Java Examples

Example 1 — Round the Quotient Up, for Any Signs

Positive and negative operands, plus a long division. This example needs Java 18 or newer.

Java
public class MathCeilDivExactBasics {
    public static void main(String[] args) {
        System.out.println("23 / 10   = " + Math.ceilDivExact(23, 10));
        System.out.println("20 / 10   = " + Math.ceilDivExact(20, 10));
        System.out.println("-23 / 10  = " + Math.ceilDivExact(-23, 10));
        System.out.println("23 / -10  = " + Math.ceilDivExact(23, -10));
        System.out.println("-23 / -10 = " + Math.ceilDivExact(-23, -10));
        System.out.println("long      = " + Math.ceilDivExact(10_000_000_001L, 10L));
    }
}

How It Works

  1. Any remainder rounds up. 23 divided by 10 is 2.3, which rounds up to 3, while 20 divided by 10 is exactly 2 and stays 2.
  2. Up is toward positive infinity. -2.3 rounds up to -2, and so does 23 divided by -10; the case -23 / -10 is +2.3 and rounds up to 3.
  3. A long division. 10000000001 divided by 10 is 1000000000.1, so the long overload rounds it up to 1000000001.

Example 2 — How Many Boxes? And Why the Old Trick Can Fail

Pack orders into boxes of 48, then compare (n + 9) / 10 with ceilDivExact for the biggest possible int. Java 18+.

Java
public class MathCeilDivExactBoxes {
    public static void main(String[] args) {
        int perBox = 48;
        int[] orders = {0, 96, 97, 130};
        for (int count : orders) {
            System.out.println(count + " items -> " + Math.ceilDivExact(count, perBox) + " boxes");
        }

        int items = Integer.MAX_VALUE;
        System.out.println("old trick:    " + ((items + 9) / 10));
        System.out.println("ceilDivExact: " + Math.ceilDivExact(items, 10));
    }
}

How It Works

  1. Exactly full vs one over. 96 items fill exactly 2 boxes of 48, while 97 items need a third box; 0 items need none, and 130 divided by 48 is about 2.7, so it also rounds up to 3.
  2. The trick overflows. Integer.MAX_VALUE + 9 wraps to -2147483640, and dividing that by 10 gives -214748364, a negative number of boxes.
  3. ceilDivExact is correct. 2147483647 divided by 10 is 214748364.7, which rounds up to 214748365; no intermediate addition is needed, so nothing can overflow.

Example 3 — Zero Divisor and MIN_VALUE / -1

The inputs that make ceilDivExact throw, compared with plain ceilDiv. Java 18+.

Java
public class MathCeilDivExactEdges {
    public static void main(String[] args) {
        int smallest = Integer.MIN_VALUE;
        System.out.println("plain ceilDiv: " + Math.ceilDiv(smallest, -1));
        try {
            Math.ceilDivExact(smallest, -1);
        } catch (ArithmeticException e) {
            System.out.println("ceilDivExact:  " + e.getMessage());
        }
        try {
            Math.ceilDivExact(Long.MIN_VALUE, -1L);
        } catch (ArithmeticException e) {
            System.out.println("long version:  " + e.getMessage());
        }
        try {
            Math.ceilDivExact(5, 0);
        } catch (ArithmeticException e) {
            System.out.println("zero divisor:  " + e.getMessage());
        }
    }
}

How It Works

  1. The wrap of the plain version. The true quotient of -2147483648 and -1 is 2147483648, which does not fit, so Math.ceilDiv returns -2147483648 without any warning.
  2. The exact version objects. ceilDivExact throws integer overflow, and the long pair with Long.MIN_VALUE throws long overflow.
  3. Division by zero. A divisor of 0 is rejected with the usual / by zero message before any rounding is attempted.

Frequently Asked Questions

It divides two ints or two longs and rounds the quotient up toward positive infinity. It throws ArithmeticException for a zero divisor and for the overflow case MIN_VALUE / -1.
Java 18, along with ceilDiv, ceilMod, floorDivExact, and divideExact. On older versions use (a + b - 1) / b for small positive values.
Use Math.ceilDivExact(items, perBox) or Math.ceilDiv(items, perBox). It rounds up, so 97 items in boxes of 48 gives 3.
They return the same value, except for Integer.MIN_VALUE / -1 and Long.MIN_VALUE / -1L. ceilDiv wraps to MIN_VALUE, while ceilDivExact throws ArithmeticException.
It rounds toward positive infinity, so ceilDivExact(-23, 10) is -2 and ceilDivExact(23, -10) is -2 as well. Only results that are exact whole numbers stay unchanged.

Next: ceilMod()

The remainder that goes with ceilDiv. Its sign is opposite to the divisor (Java 18+).

ceilMod() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful