The Math.floor() method returns the largest double value less than or equal to the argument and equal to an integer.
Direction of Rounding: Always rounds downward toward negative infinity (-∞). Returns a double (e.g. 20.0). For upward rounding, see Math.ceil(). For nearest integers, see Math.round(). For integer floor division, see Math.floorDiv().
Defined statically in java.lang.Math:
// Formal signature of Math.floor() in java.lang.Math
public static double floor(double a)
| Signature Element | Type | Description & Operational Rule |
|---|---|---|
a |
double |
The numeric value to round downward toward negative infinity. |
| Return Value | double |
Largest double value <= a that equals a mathematical integer. |
| Return Type Note | Double Primitive | Returns double (e.g. 20.0), not an integer primitive. |
Positive decimal values discard their fractional portions and round down:
// Positive numbers round down towards negative infinity
System.out.println("floor(20.0): " + Math.floor(20.0)); // 20.0
System.out.println("floor(20.12): " + Math.floor(20.12)); // 20.0
System.out.println("floor(20.56): " + Math.floor(20.56)); // 20.0
System.out.println("floor(20.99): " + Math.floor(20.99)); // 20.0Terminal Output:
floor(20.0): 20.0
floor(20.12): 20.0
floor(20.56): 20.0
floor(20.99): 20.0
Because rounding proceeds toward negative infinity, negative decimals move further negative (-20.12 becomes -21.0):
// Negative numbers round towards negative infinity (left on the number line)
System.out.println("floor(-20.0): " + Math.floor(-20.0)); // -20.0
System.out.println("floor(-20.12): " + Math.floor(-20.12)); // -21.0 (NOT -20.0!)
System.out.println("floor(-20.56): " + Math.floor(-20.56)); // -21.0
System.out.println("floor(-20.99): " + Math.floor(-20.99)); // -21.0Terminal Output:
floor(-20.0): -20.0
floor(-20.12): -21.0
floor(-20.56): -21.0
floor(-20.99): -21.0
Handling IEEE 754 edge cases:
// Special IEEE 754 floating-point edge cases
System.out.println("floor(-0.0): " + Math.floor(-0.0)); // -0.0
System.out.println("floor(+0.0): " + Math.floor(+0.0)); // 0.0
System.out.println("floor(+Infinity): " + Math.floor(Double.POSITIVE_INFINITY)); // Infinity
System.out.println("floor(-Infinity): " + Math.floor(Double.NEGATIVE_INFINITY)); // -Infinity
System.out.println("floor(NaN): " + Math.floor(Double.NaN)); // NaNTerminal Output:
floor(-0.0): -0.0
floor(+0.0): 0.0
floor(+Infinity): Infinity
floor(-Infinity): -Infinity
floor(NaN): NaN
Casting an int truncates toward zero, whereas Math.floor() rounds toward negative infinity:
// Contrasting (int) truncation vs. Math.floor() for negative numbers
double negVal = -4.2;
int castVal = (int) negVal; // Truncates toward zero -> -4
int floorVal = (int) Math.floor(negVal); // Rounds toward -Infinity -> -5
System.out.println("(int) -4.2: " + castVal); // -4
System.out.println("Math.floor(-4.2): " + floorVal); // -5Terminal Output:
(int) -4.2: -4
Math.floor(-4.2): -5
Contrasting rounding behaviors across positive and negative inputs:
| Input Value | Math.floor() | Math.ceil() | Math.round() | (int) Cast Truncation |
|---|---|---|---|---|
3.2 |
3.0 |
4.0 |
3 |
3 |
3.8 |
3.0 |
4.0 |
4 |
3 |
-3.2 |
-4.0 |
-3.0 |
-3 |
-3 |
-3.8 |
-4.0 |
-3.0 |
-4 |
-3 |
Using Math.floor() prevents tile-offset glitches when handling negative game coordinates:
public class GridCoordinateSnapper {
public static int getTileIndex(double worldCoord, double tileSize) {
// Math.floor ensures negative coordinates snap to the correct lower tile
return (int) Math.floor(worldCoord / tileSize);
}
public static void main(String[] args) {
// Tile size 32.0 pixels
System.out.println("Tile at +45.0px: " + getTileIndex(45.0, 32.0)); // Tile 1
System.out.println("Tile at +10.0px: " + getTileIndex(10.0, 32.0)); // Tile 0
System.out.println("Tile at -10.0px: " + getTileIndex(-10.0, 32.0)); // Tile -1 (CORRECT)
}
}Terminal Output:
Tile at +45.0px: 1
Tile at +10.0px: 0
Tile at -10.0px: -1
Snapping continuous readings downward to discrete intervals (e.g. multiples of 5):
public class StepValueQuantizer {
public static double snap(double val, double step) {
return Math.floor(val / step) * step;
}
public static void main(String[] args) {
System.out.println("Snap 17.8: " + snap(17.8, 5.0)); // 15.0
System.out.println("Snap 24.1: " + snap(24.1, 5.0)); // 20.0
}
}Terminal Output:
Snap 17.8: 15.0
Snap 24.1: 20.0
Assuming Math.floor(-2.1) returns -2.0. Because -3 is less than -2.1, the result is -3.0.
Math.floor() returns a double. Writing int val = Math.floor(x); fails compilation without an explicit cast: (int) Math.floor(x).
You can compile and run this complete verification class locally in any Java IDE or environment:
public class MathFloorFullDemo {
public static void main(String[] args) {
// 1. Verify positive floor
if (Math.floor(5.9) == 5.0) {
System.out.println("Check 1 Passed: 5.9 rounded down to 5.0.");
}
// 2. Verify negative floor leftward shift
if (Math.floor(-5.1) == -6.0) {
System.out.println("Check 2 Passed: -5.1 rounded down to -6.0.");
}
// 3. Verify exact integer identity
if (Math.floor(8.0) == 8.0) {
System.out.println("Check 3 Passed: Integer values unchanged.");
}
System.out.println("All Math.floor() verification checks passed successfully!");
}
}Terminal Output:
Check 1 Passed: 5.9 rounded down to 5.0.
Check 2 Passed: -5.1 rounded down to -6.0.
Check 3 Passed: Integer values unchanged.
All Math.floor() verification checks passed successfully!
Math.floor() operates on double values, while Math.floorDiv() operates directly on integer primitives.
Casting to int truncates toward zero (moves right toward 0), while Math.floor() rounds toward negative infinity (moves left).
Returns -0.0, preserving the IEEE 754 negative zero sign.
Use Math.round().
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.