all Python 3mathterm
math.floor
rounding
Rounds down to the nearest integer. math.floor(2.9) → 2, math.floor(-2.9) → -3.
Syntax
math.floor(x)
It returns an int, not a float, so the result is immediately usable as an index. On negatives floor parts ways with int(): int(-3.1) is -3 because it merely drops the fraction, while floor goes down to -4 — floor is the twin of the // operator, not of truncation. For Fraction and Decimal it calls their own __floor__, so there is no lossy detour through float.
Examples
import math
print(math.floor(3.9)) # → 3
print(math.floor(3.1)) # → 3
print(math.floor(-3.1)) # → -4
print(math.floor(-3.9)) # → -4
print(math.floor(5.0)) # → 5
print(math.floor(0.999)) # → 0
See also
Open in the full glossarydocs.python.orgReport an inaccuracy