Python math.floor() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math module

Definition

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)
CallWhat 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

ParameterDescription
xRequired positional finite real number or object implementing __floor__().

Return Value

Return ValueDescription
intLargest 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")

How It Works

  1. Divide by bin width. Scaling scores down by ten puts 59.9 at 5.99 and 70.0 at 7.0.
  2. Floor picks bracket start. math.floor(5.99) returns 5, placing 59.9 cleanly in the 50s bracket.
  3. Exact edges included. Whole multiples like 70.0 stay in their own 70s bucket.

Example 2 — Compare floor(), int(), and Floor Division on Negatives

Verify how math.floor(a / b) matches a // b while differing from int(a / b).

python
import math

for x in [-2.5, -0.1, 2.5]:
    print(f"{x:>4}: floor={math.floor(x):>2} int={int(x):>2}")

print("floor(-5 / 2):", math.floor(-5 / 2))
print("-5 // 2     :", -5 // 2)

How It Works

  1. Down vs toward zero. On -0.1, floor() moves down to -1 while int() chops to 0.
  2. Consistent with //. Both math.floor(-5 / 2) and integer -5 // 2 give -3.
  3. Positive agreement. For non-negative values like 2.5, floor() and int() match.

Example 3 — Round Monetary Amounts Down and Catch Special Float Errors

Round balances down to two decimal places and catch non-finite inputs.

python
import math

for amount in [12.349, 0.999]:
    floored = math.floor(amount * 100) / 100.0
    print(f"{amount:.3f} -> {floored:.2f}")

for bad_v in [float("-inf"), float("nan")]:
    try:
        math.floor(bad_v)
    except (OverflowError, ValueError):
        print(f"floor failed for {bad_v}")

How It Works

  1. Shift to whole cents. Multiplying by 100 before math.floor() discards fractional mills cleanly.
  2. Negative infinity rejected. Cannot convert infinite value to Python int, raising OverflowError.
  3. NaN rejected. Passing nan raises ValueError.

Frequently Asked Questions

math.floor(-2.5) rounds downward toward negative infinity and returns -3. int(-2.5) truncates toward zero and returns -2.
In Python 3, math.floor() always returns an int.
For small numbers yes, though for huge integers a / b converts to float and can lose precision, whereas a // b stays exact.
It raises OverflowError because infinity cannot be represented as an integer.
Multiply by 100, call math.floor(), and divide by 100.0 (or use decimal.Decimal with ROUND_FLOOR for exact financial math).

Next: math.fma()

Return fused multiply-add (x * y) + z with a single rounding (Python 3.13+).

fma() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful