math.floor(x) returns the floor of x: the largest integer less than or equal to x, returned as an int.
Unlike built-in int() or math.trunc(), which chop toward zero, math.floor() always rounds downward toward negative infinity—so math.floor(-2.5) is -3, matching Python's // operator.
math.floor() is a function in Python's standard math module: import it with import math and call math.floor(...), or import the name directly with from math import floor.
Syntax
Import math and pass any finite real number.
python
import math
n = math.floor(x)
Call
What it does
math.floor(2.9)
Returns 2.
math.floor(-2.5)
Returns -3 (downward toward -inf).
math.floor(5.0)
Returns 5 (an int).
math.floor(float("nan"))
Raises ValueError: cannot convert NaN.
Both math.floor(a / b) and a // b round toward negative infinity, though a // b keeps an exact integer step without float rounding.
Function signatures
python
math.floor(x, /)
# returns: int largest integer <= x (calls x.__floor__())
Parameters
Parameter
Description
x
Required positional finite real number or object implementing __floor__().
Return Value
Return Value
Description
int
Largest integer not exceeding x.
Python Examples
Example 1 — Group Exam Scores Into 10-Point Histogram Buckets
Divide scores by 10 and floor them into lower bracket bounds.
python
import math
for score in [59.9, 70.0, 84.5, 99.9]:
bucket = math.floor(score / 10.0) * 10
print(f"score={score:>4} -> {bucket}s bracket")