Math.floor()
Rounds down to the nearest integer that is less than or equal.
Available since: Java 1.0
Syntax
static double floor(double a)Parameters
| Parameter | Type | Description |
|---|---|---|
| a | double | A decimal number. |
Return value
doubleA whole value as a double.
Examples
Down means towards minus infinity:
void main() {
IO.println(Math.floor(3.7));
IO.println(Math.floor(-3.2));
IO.println((int) -3.7);
}Output:
3.0
-4.0
-3Notes
The (int) cast drops the fractional part, i.e. rounds towards zero: for negative numbers that differs from floor(). The result of floor() is a double; cast it to get an int.
See also
- Math.ceil() — Rounds up to the nearest integer that is greater than or equal.
- Math.round() — Rounds a number to the nearest integer.
- Math.floorMod() — A remainder that always has the sign of the divisor.
Related recipes
In the tutorial
- Type conversion — Automatic widening, explicit (int) casts, turning strings into numbers with Integer.parseInt and numbers into strings.