In Java, Math.floorDiv(x, y) divides x by y and rounds the result down, toward negative infinity. For positive numbers it matches the / operator, but for a negative result it does not: -7 / 2 is -3, while Math.floorDiv(-7, 2) is -4.
That makes it the right tool whenever negative numbers must keep working in a regular pattern: grid cells left of zero, whole minutes before a start time, or the day number for a date before the epoch. It pairs with floorMod.
A static method of the java.lang.Math class, added in Java 8 (JDK 1.8). Call it on the class name, as in Math.floorDiv(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass the dividend, then the divisor. The result is an int for two ints and a long if any argument is a long.
Java
int a = Math.floorDiv(7, 2); // 3
int b = Math.floorDiv(-7, 2); // -4 (plain -7 / 2 is -3)
int c = Math.floorDiv(7, -2); // -4
long d = Math.floorDiv(-10_000_000_000L, 3); // -3333333334
Call
What it does
Math.floorDiv(7, 2)
3.5 rounded down gives 3.
Math.floorDiv(-7, 2)
-3.5 rounded down gives -4.
Math.floorDiv(x, cellSize)
Grid cell number, also for negative x.
Math.floorDiv(5, 0)
Throws ArithmeticException with the message / by zero.
floorDiv(int, int) and floorDiv(long, long) exist since Java 8. The (long, int) overload was added in Java 9; on Java 8 an int divisor is simply widened to long, and the result is the same.
Library declarations
Java
// java.lang.Math
public static int floorDiv(int x, int y)
public static long floorDiv(long x, int y)
public static long floorDiv(long x, long y)
Parameters
Parameter
Description
x
The dividend (an int or a long).
y
The divisor (an int or a long). Must not be zero.
Return Value
Return Value
Description
int / long
The largest integer that is less than or equal to the exact quotient x / y. It is long for the overloads with a long dividend.
Throws ArithmeticException
When y is zero. The message is / by zero.
Compare with Similar Methods
Ways to divide integers and choose the rounding:
Method
How it differs
x / y
Rounds toward zero; same as floorDiv unless the result is negative.
Negative results differ from plain. The exact quotients -3.5 round down to -4, while the operator gives -3; the remainders 1 and -1 compensate for that difference.
Positive results agree. For 7 / 2 and -7 / -2 the exact quotient is +3.5, which both approaches turn into 3, and floorMod follows the divisor's sign (1 and -1).
The rebuild always works. For example -4 * 2 + 1 is -7 and 3 * -2 + -1 is -7, so each quotient/remainder pair gives back the original dividend.
Example 3 — Zero Divisor, MIN_VALUE, and long Values
The inputs that behave unusually, plus a negative long and a negative time offset.
Error: / by zero
MIN_VALUE / -1: -2147483648
long: -3333333334
-90 seconds in whole minutes: -2
How It Works
Zero is rejected. Dividing by 0 raises ArithmeticException; the message / by zero is the same text the / operator uses.
The one silent overflow. +2147483648 does not fit in an int, so the quotient of Integer.MIN_VALUE and -1 wraps back to -2147483648.
Rounding down on the long side. -10000000000 / 3 is -3333333333.33..., so the result is -3333333334, and -90 / 60 is -1.5, which floors to -2 whole minutes.
Frequently Asked Questions
It divides two integers and rounds the quotient down toward negative infinity. Math.floorDiv(-7, 2) returns -4, while -7 / 2 returns -3.
The / operator truncates toward zero, and floorDiv rounds toward negative infinity. They give the same answer unless the exact quotient is negative and not a whole number.
floorDiv(int, int) and floorDiv(long, long) were added in Java 8. The floorDiv(long, int) overload came in Java 9. floorDivExact is newer (Java 18).
They always satisfy floorDiv(x, y) * y + floorMod(x, y) == x. floorDiv gives the rounded-down quotient and floorMod gives the matching remainder, which has the sign of the divisor.
A zero divisor throws ArithmeticException with the message / by zero. Only Math.floorDiv(Integer.MIN_VALUE, -1) overflows silently and returns Integer.MIN_VALUE.