Java Math floorDiv() Method

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

Definition

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

ParameterDescription
xThe dividend (an int or a long).
yThe divisor (an int or a long). Must not be zero.

Return Value

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

Compare with Similar Methods

Ways to divide integers and choose the rounding:

MethodHow it differs
x / yRounds toward zero; same as floorDiv unless the result is negative.
floorMod(x, y)The remainder that pairs with floorDiv.
ceilDiv(x, y)Rounds the quotient up instead of down. Java 18+.
(int) Math.floor(a / (double) b)The old way with doubles: more typing and less exact for huge long values.

Java Examples

Example 1 — Which Grid Cell Is This Coordinate In?

A map is split into cells 5 units wide. Plain / puts every point just left of zero in the wrong cell.

Java
public class MathFloorDivGrid {
    public static void main(String[] args) {
        int cellSize = 5;
        int[] xs = {7, 4, 0, -1, -5, -6};
        for (int x : xs) {
            System.out.println("x=" + x + " cell " + Math.floorDiv(x, cellSize)
                + " (plain / gives " + x / cellSize + ")");
        }
    }
}

How It Works

  1. Right of zero they agree. 7, 4, and 0 give cells 1, 0, and 0 with both methods, because the exact quotient is never negative.
  2. The seam at zero. -1 / 5 is -0.2; plain / truncates it to 0, merging x = -1 into the cell of x = 0, while floorDiv rounds down to -1.
  3. Equal-width cells. With floorDiv cell -1 holds exactly -5 to -1 and cell -2 starts at -6, so every cell contains five values, as on the positive side.

Example 2 — Rebuild the Dividend From floorDiv and floorMod

Run all four sign combinations of 7 and 2. The last column proves floorDiv * y + floorMod == x.

Java
public class MathFloorDivPairs {
    public static void main(String[] args) {
        int[][] pairs = {{-7, 2}, {7, -2}, {7, 2}, {-7, -2}};
        for (int[] p : pairs) {
            int q = Math.floorDiv(p[0], p[1]);
            int r = Math.floorMod(p[0], p[1]);
            System.out.println(p[0] + " / " + p[1] + " -> floorDiv " + q
                + " (plain " + p[0] / p[1] + "), floorMod " + r
                + ", rebuilt " + (q * p[1] + r));
        }
    }
}

How It Works

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

Java
public class MathFloorDivEdges {
    public static void main(String[] args) {
        try {
            Math.floorDiv(5, 0);
        } catch (ArithmeticException e) {
            System.out.println("Error: " + e.getMessage());
        }
        System.out.println("MIN_VALUE / -1: " + Math.floorDiv(Integer.MIN_VALUE, -1));
        System.out.println("long: " + Math.floorDiv(-10_000_000_000L, 3));
        System.out.println("-90 seconds in whole minutes: " + Math.floorDiv(-90, 60));
    }
}

How It Works

  1. Zero is rejected. Dividing by 0 raises ArithmeticException; the message / by zero is the same text the / operator uses.
  2. The one silent overflow. +2147483648 does not fit in an int, so the quotient of Integer.MIN_VALUE and -1 wraps back to -2147483648.
  3. 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.

Next: floorDivExact()

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

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

10 people found this page helpful