The Math.floorDiv() method returns the largest integer value that is less than or equal to the algebraic quotient.
Floored Division vs. Truncation: Unlike standard integer division / which truncates toward zero, floorDiv() rounds toward negative infinity (-∞). Introduced in Java 8 to support negative date calculations. For floating-point downward rounding, see Math.floor(). For upward rounding, explore Math.ceil(). For nearest-integer rounding, see Math.round().
Defined statically in java.lang.Math:
// Overloaded signatures of Math.floorDiv() in 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)
| Signature | Argument Types | Description & Operational Rule |
|---|---|---|
floorDiv(int x, int y) |
int, int |
Returns int quotient rounded down toward -∞. |
floorDiv(long x, int y) |
long, int |
Java 9 overload; returns long quotient without casting. |
floorDiv(long x, long y) |
long, long |
Returns 64-bit long quotient rounded down toward -∞. |
When the quotient is positive, Math.floorDiv() produces the exact same result as the / operator:
// For positive results, floorDiv matches standard integer division /
System.out.println("20 / 7: " + (20 / 7)); // 2
System.out.println("Math.floorDiv(20, 7):" + Math.floorDiv(20, 7)); // 2Terminal Output:
20 / 7: 2
Math.floorDiv(20, 7):2
When dividing negative numbers, / truncates toward zero, whereas floorDiv() rounds down toward negative infinity:
// The Critical Difference: Negative division rounds down toward -Infinity
System.out.println("-20 / 7: " + (-20 / 7)); // -2 (truncates to 0)
System.out.println("Math.floorDiv(-20, 7):" + Math.floorDiv(-20, 7)); // -3 (floors to -Infinity)
System.out.println("7 / -2: " + (7 / -2)); // -3
System.out.println("Math.floorDiv(7, -2): " + Math.floorDiv(7, -2)); // -4Terminal Output:
-20 / 7: -2
Math.floorDiv(-20, 7):-3
7 / -2: -3
Math.floorDiv(7, -2): -4
Math.floor() performs double division and returns a double, whereas Math.floorDiv() operates natively on integer primitives:
// Comparing Math.floor() with double division vs Math.floorDiv()
System.out.println("Math.floor(20.0 / 7.0): " + Math.floor(20.0 / 7.0)); // 2.0 (double)
System.out.println("Math.floorDiv(20, 7): " + Math.floorDiv(20, 7)); // 2 (int primitive)
System.out.println("Math.floor(-20.0 / 7.0): " + Math.floor(-20.0 / 7.0)); // -3.0
System.out.println("Math.floorDiv(-20, 7): " + Math.floorDiv(-20, 7)); // -3Terminal Output:
Math.floor(20.0 / 7.0): 2.0
Math.floorDiv(20, 7): 2
Math.floor(-20.0 / 7.0): -3.0
Math.floorDiv(-20, 7): -3
floorDiv() pairs with floorMod() to satisfy the division identity (floorDiv(x, y) * y) + floorMod(x, y) == x:
// Pairing with Math.floorMod(): floorDiv(x, y) * y + floorMod(x, y) == x
int x = -20;
int y = 7;
int q = Math.floorDiv(x, y); // -3
int r = Math.floorMod(x, y); // 1
System.out.println("Quotient: " + q); // -3
System.out.println("Remainder: " + r); // 1
System.out.println("Identity: " + (q * y + r == x)); // true (-3 * 7 + 1 == -20)Terminal Output:
Quotient: -3
Remainder: 1
Identity: true
Comparing outcomes between truncating integer division and floored division:
| Expression | Exact Algebraic Value | Truncating (x / y) | Math.floorDiv(x, y) |
|---|---|---|---|
4 / 3 |
1.333... |
1 |
1 |
-4 / 3 |
-1.333... |
-1 (moves to 0) |
-2 (moves to -∞) |
-20 / 7 |
-2.857... |
-2 |
-3 |
20 / -7 |
-2.857... |
-2 |
-3 |
Computing calendar days before the 1970 Unix Epoch accurately without off-by-one errors:
public class EpochDaysCalculator {
public static long toEpochDay(long epochSecond) {
// 86,400 seconds in a day. floorDiv handles seconds before 1970 accurately!
return Math.floorDiv(epochSecond, 86400L);
}
public static void main(String[] args) {
// 10 seconds before 1970 Epoch origin (-10s)
System.out.println("Truncating division: " + (-10L / 86400L)); // 0 (WRONG DAY!)
System.out.println("floorDiv day index: " + toEpochDay(-10L)); // -1 (CORRECT: Dec 31, 1969)
}
}Terminal Output:
Truncating division: 0
floorDiv day index: -1
Preventing tile-coordinate clipping when mapping negative coordinates in games and charts:
public class GridSnapCalculator {
public static int getTileCoordinate(int pixelCoord, int tileSize) {
// Works seamlessly across negative and positive screen coordinates
return Math.floorDiv(pixelCoord, tileSize);
}
public static void main(String[] args) {
System.out.println("Tile at +35px (size 16): " + getTileCoordinate(35, 16)); // Tile 2
System.out.println("Tile at -5px (size 16): " + getTileCoordinate(-5, 16)); // Tile -1
}
}Terminal Output:
Tile at +35px (size 16): 2
Tile at -5px (size 16): -1
Calling Math.floorDiv(Integer.MIN_VALUE, -1) overflows 32-bit int and returns Integer.MIN_VALUE without throwing an error.
Passing 0 as the divisor throws an ArithmeticException: / by zero.
You can compile and run this complete verification class locally in any Java IDE or environment:
public class MathFloorDivFullDemo {
public static void main(String[] args) {
// 1. Verify positive integer floor division
if (Math.floorDiv(14, 3) == 4) {
System.out.println("Check 1 Passed: Positive floorDiv evaluates to 4.");
}
// 2. Verify negative downward rounding
if (Math.floorDiv(-14, 3) == -5) {
System.out.println("Check 2 Passed: Negative floorDiv rounds down to -5.");
}
// 3. Verify zero division throws ArithmeticException
try {
Math.floorDiv(10, 0);
} catch (ArithmeticException e) {
System.out.println("Check 3 Passed: Division by zero caught.");
}
System.out.println("All Math.floorDiv() verification checks passed successfully!");
}
}Terminal Output:
Check 1 Passed: Positive floorDiv evaluates to 4.
Check 2 Passed: Negative floorDiv rounds down to -5.
Check 3 Passed: Division by zero caught.
All Math.floorDiv() verification checks passed successfully!
To support the java.time package, where calculating calendar days from negative epoch timestamps requires floored rather than truncated division.
Standard integer division truncates towards zero (e.g. -7 / 2 == -3), while Math.floorDiv(-7, 2) == -4.
No. Math.floorDiv() operates on int and long. For floating point, use Math.floor(double).
Use Math.floorMod(x, y), which satisfies floorDiv(x, y) * y + floorMod(x, y) == x.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.