Java Math floorDivExact() Method

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

Definition

In Java, Math.floorDivExact(x, y) divides two whole numbers and rounds the quotient down, toward negative infinity. Math.floorDivExact(-17, 5) is -4, while the plain -17 / 5 is -3 because / rounds toward zero.

It is the overflow-checked sibling of floorDiv. The only overflowing input is MIN_VALUE divided by -1; there floorDivExact 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.floorDivExact(...). No import is needed, because classes in java.lang are always visible. Requires Java 18 or newer.

Syntax

Pass the dividend first and the divisor second, both int or both long. The result is the largest whole number not greater than the exact quotient.

Java
int a = Math.floorDivExact(17, 5);                    // 3
int b = Math.floorDivExact(-17, 5);                   // -4
long day = Math.floorDivExact(seconds, 86_400L);      // day number
Math.floorDivExact(5, 0);                             // throws ArithmeticException
CallWhat it does
Math.floorDivExact(17, 5)int overload: 3.4 rounds down to 3.
Math.floorDivExact(-17, 5)Down means toward negative infinity: -3.4 becomes -4.
Math.floorDivExact(x, 1000L)long overload: cell number of a long coordinate.
Math.floorDivExact(Long.MIN_VALUE, -1L)Throws ArithmeticException ("long overflow").

For non-negative operands, floorDivExact and the / operator agree. They differ when exactly one operand is negative and there is a remainder. The matching remainder method is floorMod, so x == floorDivExact(x, y) * y + floorMod(x, y).

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

Parameters

ParameterDescription
xThe dividend (the number to divide).
yThe divisor (the number to divide by). Must not be zero.

Return Value

Return ValueDescription
int / longThe quotient rounded down: the largest whole number that is less 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 rules for integer division, side by side:

MethodHow it differs
x / yRounds toward zero: -17 / 5 is -3. No overflow check.
floorDiv(x, y)Same rounding as floorDivExact, but wraps on MIN_VALUE / -1.
ceilDivExact(x, y)Rounds up instead of down; also overflow-checked.
divideExact(x, y)Rounds toward zero, with the same overflow check.

Java Examples

Example 1 — Round the Quotient Down, for Any Signs

All four sign combinations, plus a long division. This example needs Java 18 or newer.

Java
public class MathFloorDivExactBasics {
    public static void main(String[] args) {
        System.out.println("17 / 5   = " + Math.floorDivExact(17, 5));
        System.out.println("-17 / 5  = " + Math.floorDivExact(-17, 5));
        System.out.println("17 / -5  = " + Math.floorDivExact(17, -5));
        System.out.println("-17 / -5 = " + Math.floorDivExact(-17, -5));
        System.out.println("long     = " + Math.floorDivExact(7_000_000_000L, 2L));
    }
}

How It Works

  1. Positive quotient. 17 divided by 5 is 3.4, and rounding down gives 3, the same as the plain / operator would.
  2. Mixed signs round away from zero. -3.4 and 17 divided by -5 (also -3.4) both round down to -4, the next smaller whole number.
  3. Two negatives. -17 divided by -5 is +3.4, so rounding down again gives 3. The long line is exact: 7000000000 divided by 2 is 3500000000.

Example 2 — Day Numbers From Seconds, Before and After 1970

Convert seconds since the epoch to a day number. Plain / puts the last second before day 0 into the wrong day. Java 18+.

Java
public class MathFloorDivExactDays {
    public static void main(String[] args) {
        long[] seconds = {90_000L, 86_399L, -1L, -86_400L, -86_401L};
        for (long s : seconds) {
            System.out.println(s + " s -> / gives " + (s / 86_400L)
                    + ", floorDivExact gives " + Math.floorDivExact(s, 86_400L));
        }
    }
}

How It Works

  1. After the epoch, both agree. 90000 seconds is 1.04 days, so both give day 1, and 86399 seconds is still 0.99999 of day 0.
  2. The second before zero. -1 second is 0.00001 days before the start of day 0, so it belongs to day -1; truncation gives 0 and mislabels it, while rounding down gives -1.
  3. Whole days and just past them. -86400 is exactly -1 day, so both agree; one second earlier, -86401 is -1.00001 days, where truncation says -1 but floorDivExact correctly says -2.

Example 3 — Grid Cell Numbers From Long Coordinates

Divide each coordinate by the cell size 1000 and store the cell as an int, using toIntExact for the last step. Java 18+.

Java
public class MathFloorDivExactCells {
    public static void main(String[] args) {
        long[] xs = {2_500L, -2_500L, -2_500_000_000_000L};
        for (long x : xs) {
            try {
                int cell = Math.toIntExact(Math.floorDivExact(x, 1_000L));
                System.out.println("x=" + x + " -> cell " + cell);
            } catch (ArithmeticException e) {
                System.out.println("x=" + x + " -> " + e.getMessage());
            }
        }
        try {
            Math.floorDivExact(Long.MIN_VALUE, -1L);
        } catch (ArithmeticException e) {
            System.out.println("MIN / -1 -> " + e.getMessage());
        }
    }
}

How It Works

  1. Positive coordinate. 2500 divided by 1000 is 2.5, so it lies in cell 2, the cell covering 2000 to 2999.
  2. Negative coordinate. -2500 divided by 1000 is -2.5, which rounds down to cell -3, the cell covering -3000 to -2001; the plain / operator would wrongly say -2.
  3. Two different overflows. The huge x gives the long cell -2500000000, which is too small for an int, so toIntExact reports integer overflow; the final try shows floorDivExact itself throwing long overflow for Long.MIN_VALUE / -1.

Frequently Asked Questions

It divides two ints or two longs and rounds the quotient down toward negative infinity. It throws ArithmeticException for a zero divisor and for the overflow case MIN_VALUE / -1.
Java 18. The non-exact Math.floorDiv has been available since Java 8 (the long, int overload since Java 9).
They return the same value, except for Integer.MIN_VALUE / -1 and Long.MIN_VALUE / -1L. floorDiv wraps to MIN_VALUE, while floorDivExact throws ArithmeticException.
Because it rounds down toward negative infinity. The exact quotient is -3.4, and the largest whole number not greater than that is -4. The / operator truncates toward zero and gives -3.
Use it when negative values are possible and you want "which bucket does this fall into" semantics, such as day numbers before 1970, grid cells, or pages, and you also want overflow reported.

Next: floorMod()

Remainder whose sign follows the divisor. Ideal for wrapping indexes and clocks.

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

11 people found this page helpful