Library contents

Java Math.floorDiv

Integer floor division rounds a quotient toward negative infinity.

A simple example

Math.floorDiv

  1. Start with −7 divided by 3
  2. Do this Round the quotient down
  3. Result −3
The quotient lies between −3 and −2. Its floor is the lower integer, −3.
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Math.floorDiv(-7, 3));
    }
}
Output
-3

Syntax

Java syntax
Math.floorDiv(minute, slot)
JDK 21 method signatures (3)
public static int floorDiv(int x, int y)
public static long floorDiv(long x, int y)
public static long floorDiv(long x, long y)
Run these examples in Java

Save one example at a time as Main.java. Use JDK 21.

java Main.java

Number time slots before opening

A shift planner numbers intervals relative to opening time. Assign a time before opening to the correct earlier slot rather than losing the sign at the boundary.

From the programint minute = -7
Result-3
Main.java

class Main {
    public static void main(String[] args) throws Exception {
        int minute = -7;
        int slot = 3;
        System.out.println(Math.floorDiv(minute, slot));
    }
}
Output
-3

Minutes minus nine through minus seven share slot minus three.

Advertisements

Locate an event before opening

A desk groups arrivals into ten-minute blocks, with block zero beginning at opening time. A volunteer arrived one minute before opening and needs the correct block label.

GivenMinute −1 · block width 10
Worked result−1

Floor division assigns the minute immediately before zero to block minus one.

Group an afternoon reading

A meter logs one entry per five-minute block starting at noon. A reading comes in seventeen minutes after noon.

GivenMinute 17 · block width 5
Worked result3

Block three covers minutes fifteen through nineteen.

What to watch for

Integer division with the slash operator truncates toward zero on the JVM.

Documentation for Math.floorDiv