In Java, Math.floor(a) returns the largest whole number that is less than or equal toa. It rounds down: Math.floor(4.9) is 4.0.
"Down" means toward negative infinity, so Math.floor(-2.5) is -3.0. That is the key difference from an (int) cast, which simply chops off the decimals and gives -2.
A static method of the java.lang.Math class, available since the early versions of Java (JDK 1.0). Call it on the class name, as in Math.floor(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass a double; the whole-number result is still a double.
Java
double a = Math.floor(4.9); // 4.0
double b = Math.floor(-2.5); // -3.0
int cast = (int) -2.5; // -2 (truncates toward zero)
int cell = (int) Math.floor(x / size); // grid cell, also for negative x
Call
What it does
Math.floor(7.99)
Returns 7.0.
Math.floor(-7.01)
Returns -8.0.
(int) Math.floor(seconds / 60)
Whole minutes in a number of seconds.
Math.floor(x * 100) / 100
Cut to two decimals (see the pitfall below).
For int and long division that rounds down, skip the doubles and use Math.floorDiv (Java 8+), which is exact for every value.
Library declarations
Java
// java.lang.Math
public static double floor(double a)
Parameters
Parameter
Description
a
The value to round down. Smaller number types are widened to double.
Return Value
Return Value
Description
double
The largest whole-number double that is <= a. NaN, infinities, zeros (including -0.0), and whole values come back unchanged.
Positive side agrees. 7.5 / 5.0 is 1.5; flooring and truncating both give cell 1.
Negative side differs. -0.5 / 5.0 is -0.1. floor goes down to -1, the cell that really contains -0.5, while the cast chops toward zero and wrongly lands in cell 0.
Consistent cells. With floor, every cell covers exactly 5 units ([-10, -5) is cell -2), so -7.5 is in cell -2, not -1.
Example 3 — Cutting to Two Decimals, and a Binary Surprise
Truncate prices to cents with floor(x * 100) / 100 and see where it breaks.
Cut to cents: 19.98
tricky * 100 = 114.99999999999999
Cut to cents: 1.14
floor(-0.0) = -0.0
How It Works
Shift, floor, shift back. 19.987 * 100 is about 1998.7, floor gives 1998.0, and dividing by 100 prints 19.98.
Why 1.15 fails. The double nearest to 1.15 is a hair below it, so multiplying by 100 gives 114.99999999999999, and floor drops it to 114, printing 1.14 instead of 1.15.
Zero keeps its sign.-0.0 is already a whole number, so floor returns it unchanged, minus sign included.
Frequently Asked Questions
It rounds a number down to the nearest whole number and returns it as a double. Math.floor(4.9) returns 4.0.
For positive numbers they agree. For negative numbers, floor rounds down (Math.floor(-2.5) is -3.0) while (int) -2.5 truncates toward zero and gives -2.
So that it can handle every double, including values far larger than any int or long. Cast the result when you know it fits.
Because 1.15 cannot be stored exactly in binary. The product is 114.99999999999999, which rounds down to 114. Use BigDecimal or whole cents for money.
Use Math.floorDiv(a, b). Plain a / b rounds toward zero, which differs for negative results.