Java Math ceilDiv() Method

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

Definition

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
CallWhat 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

ParameterDescription
xThe dividend: the number being divided (an int or a long).
yThe divisor: the number to divide by (an int or a long). Must not be zero.

Return Value

Return ValueDescription
int / longThe 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.
Throws ArithmeticExceptionWhen y is zero. The message is / by zero.

Compare with Similar Methods

Ways to divide integers and round the result:

MethodHow it differs
floorDiv(x, y)Rounds down (toward negative infinity) instead of up. Java 8+.
ceilDivExact(x, y)Same result, but throws on the MIN_VALUE / -1 overflow. Java 18+.
x / yPlain integer division: rounds toward zero, so positive results lose their fraction.
(int) Math.ceil(a / (double) b)The old way: needs doubles and a cast, and can lose precision for huge long values.

Java Examples

Example 1 — How Many Pages, How Many Buses?

A list of 23 items shows 10 per page, and a school trip has 130 guests for 48-seat buses. This example needs Java 18 or newer.

Java
public class MathCeilDivPages {
    public static void main(String[] args) {
        int items = 23;
        int perPage = 10;
        System.out.println("Plain division: " + items / perPage);
        System.out.println("Pages needed:   " + Math.ceilDiv(items, perPage));
        System.out.println("Exact fit (20): " + Math.ceilDiv(20, perPage));

        int guests = 130;
        int seats = 48;
        System.out.println("Buses for " + guests + " guests: " + Math.ceilDiv(guests, seats));
    }
}

How It Works

  1. The leftover item. 23 / 10 is 2.3. Plain / drops the .3 and says 2 pages, so three items would never be shown; ceilDiv rounds up to 3.
  2. No extra page when it fits. 20 items fill exactly 2 pages, so there is no remainder to round up and the answer stays 2.
  3. Seats, not decimals. 130 / 48 is about 2.71 buses, and two buses carry only 96 guests, so ceilDiv returns 3 as an int with no casts and no doubles.

Example 2 — ceilDiv, floorDiv, and Plain Division Side by Side

See which way each method rounds when the signs differ, and call the long overload. Java 18+.

Java
public class MathCeilDivNegatives {
    public static void main(String[] args) {
        int[][] pairs = {{-7, 2}, {7, -2}, {-7, -2}, {7, 2}};
        for (int[] p : pairs) {
            System.out.println(p[0] + " / " + p[1]
                + ": ceilDiv=" + Math.ceilDiv(p[0], p[1])
                + ", floorDiv=" + Math.floorDiv(p[0], p[1])
                + ", plain=" + p[0] / p[1]);
        }
        long big = Math.ceilDiv(10_000_000_000L, 3);
        System.out.println("long: " + big);
    }
}

How It Works

  1. 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.
  2. 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.
  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

The two inputs you must watch for. Java 18+.

Java
public class MathCeilDivEdges {
    public static void main(String[] args) {
        try {
            Math.ceilDiv(5, 0);
        } catch (ArithmeticException e) {
            System.out.println("Error: " + e.getMessage());
        }
        System.out.println("MIN_VALUE / -1: " + Math.ceilDiv(Integer.MIN_VALUE, -1));
        System.out.println("Zero items: " + Math.ceilDiv(0, 7));
        System.out.println("1 item, 1000 per box: " + Math.ceilDiv(1, 1000));
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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.

Next: ceilDivExact()

Like ceilDiv, but throws ArithmeticException on overflow (Java 18+).

ceilDivExact() 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.

7 people found this page helpful