Python floor() Function: math.floor() Syntax and 8 Examples

If you have a decimal number in Python and you want the whole number below it, the Python floor function, math.floor(), is the quickest way to get it. It takes a number and gives you back the largest integer not above it.

import math
print(math.floor(7.9))
# 7

In this tutorial, I’ll run Python’s floor() against whole numbers, negative values, budgets, decimal places, buckets, lists, NumPy and pandas, plus the errors you’ll hit. Every example ran in Python 3.12.5 in VS Code.

Python math.floor() Syntax

floor() lives in the math module, so you import math first (or write from math import floor):

math.floor(x)
ParameterRequiredDefaultDescription
xYesnoneAn int, float, Decimal, Fraction or any object with a __floor__() method

Return value: an int, the largest integer less than or equal to x. For anything that isn’t a float, Python calls x.__floor__() and returns what it gives back.

Note: in Python 2, math.floor() returned a float. In Python 3 it returns an int, so you don’t need an extra int() call. The Python math module also has ceil() and trunc() for the other directions.

When to Use floor() in Python

  • Counting how many full items, shares or tickets a budget can buy
  • Grouping ages, prices or scores into buckets of 10, 100 or 1,000
  • Turning minutes or seconds into whole hours
  • Cutting a price down to cents without ever rounding up
  • Getting an int from a float when negative numbers are possible

Flooring in Python always moves toward negative infinity. If you only need to drop the decimals, see how to convert a float to an int in Python.

Example 1: Floor a Number in Python with math.floor()

Let me start with a few plain calls, including a whole number and a negative one:

import math

print(math.floor(3.7))
print(math.floor(9.99))
print(math.floor(7))
print(math.floor(-2.3))
print(type(math.floor(3.7)).__name__)

Output:

3
9
7
-3
int
Python math.floor rounding 3.7, 9.99, 7 and -2.3 down in VS Code
math.floor() always rounds down and returns an int.

How does this work? 3.7 became 3 and 9.99 became 9, even though 9.99 is almost 10. The int 7 came back unchanged. -2.3 went to -3, because -3 is the next integer below it. The last line confirms the result type is int.

Pro Tip: floor in Python never looks at how close the value is to the next number. If you want the nearest number instead, that’s a job for round().

Example 2: Python Floor to Int with Negative Numbers

Many people use int() to get a whole number. For positive values it matches floor(), but negatives tell a different story. Here I compare floor(), int() and math.trunc():

import math

for x in (4.7, -4.7, -0.5):
    print(x, math.floor(x), int(x), math.trunc(x))

Output:

4.7 4 4 4
-4.7 -5 -4 -4
-0.5 -1 0 0
Python math.floor vs int and math.trunc on 4.7, -4.7 and -0.5
floor() and int() split on negative numbers.

How does this work? for 4.7 all three return 4. For -4.7, floor() gives -5 while int() and trunc() give -4, since they just cut off the decimals. Even -0.5 floors to -1, while int() returns 0.

So if your Python data can go below zero (temperatures, account balances, coordinates), use math.floor() when you mean round down.

Example 3: Floor the Result of a Division

Say you have $1,000 and one share costs $123.45. You can’t buy part of a share here, so you want the whole number of shares. I also show the // operator for comparison:

import math

budget = 1_000.00
price = 123.45          # one share, in USD

print(budget / price)
print(math.floor(budget / price))
print(budget // price)

left = budget - math.floor(budget / price) * price
print(round(left, 2))

Output:

8.100445524503847
8
8.0
12.4
Python math.floor counting full shares from a 1000 dollar budget compared with floor division
floor() gives 8 shares; // gives 8.0.

How does this work? the plain division gave 8.100445524503847. math.floor() turned it into the int 8. The // operator also floors, but with float inputs it returns the float 8.0. The leftover cash after buying 8 shares is $12.40.

If you want the full story on / versus //, read normal division vs floor division in Python.

Example 4: Floor a Number to 2 Decimal Places

math.floor() has no digits argument, but you can scale the number, floor it, and scale it back. This keeps prices from ever rounding up:

import math
from decimal import Decimal, ROUND_FLOOR

def floor_to(x, places):
    factor = 10 ** places
    return math.floor(x * factor) / factor

print(floor_to(19.987, 2))
print(floor_to(3.14159, 3))
print(floor_to(-1.234, 1))
print(floor_to(4.35, 2))
print(Decimal("4.35").quantize(Decimal("0.01"), rounding=ROUND_FLOOR))

Output:

19.98
3.141
-1.3
4.34
4.35
Python floor to decimal places with a helper function and Decimal ROUND_FLOOR
4.35 floors to 4.34 because of float storage.

How does this work? 19.987 became 19.98 and 3.14159 became 3.141. -1.234 went down to -1.3. But look at 4.35: it gave 4.34, because 4.35 * 100 is stored as a hair under 435.

That’s a floating-point quirk in Python, not a floor() bug. For money, use Decimal with ROUND_FLOOR, which gave the correct 4.35 in the last line.

Example 5: Group Values into Buckets with floor()

floor() is handy for grouping. Here I put ages into decades and turn 437 minutes into hours and minutes:

import math

ages = [23, 45, 67, 34, 29, 54, 76, 38]
print([math.floor(a / 10) * 10 for a in ages])

minutes = 437
print(math.floor(minutes / 60), "h", minutes % 60, "min")
print(divmod(minutes, 60))

Output:

[20, 40, 60, 30, 20, 50, 70, 30]
7 h 17 min
(7, 17)

How does this work? dividing by 10, flooring, and multiplying by 10 snaps 67 to 60 and 29 to 20. For time, 437 / 60 floors to 7 hours, and the % operator gives the 17 leftover minutes.

Pro Tip: Python’s built-in divmod() returns both parts at once, (7, 17), which reads cleaner when you need the quotient and the remainder. The bucket line uses a list comprehension.

Example 6: Floor Every Number in a List

To floor a whole list of readings, use a list comprehension or map(). These are Chicago temperatures in °F, including one below zero:

import math

temps = [41.8, 38.2, 45.9, 43.5, -2.4]   # Chicago readings, °F

print([math.floor(t) for t in temps])
print(list(map(math.floor, temps)))
print(sum(map(math.floor, temps)))

Output:

[41, 38, 45, 43, -3]
[41, 38, 45, 43, -3]
164

How does this work? both lines give [41, 38, 45, 43, -3]. Notice -2.4 became -3. The Python map() function applies math.floor to each item, and sum() adds the floored values to 164.

Example 7: NumPy floor and pandas .floor()

math.floor() takes one number at a time. For arrays, use NumPy’s np.floor(), which also works on a pandas Series. pandas timestamps have their own .floor() method too:

import numpy as np
import pandas as pd

arr = np.array([2.7, -1.2, 5.0, 3.999])
print(np.floor(arr))
print(np.floor(arr).astype(int))

prices = pd.Series([19.99, 7.5, -3.2])
print(np.floor(prices).tolist())

ts = pd.Timestamp("2026-11-27 14:47:30")
print(ts.floor("h"))

Output:

[ 2. -2.  5.  3.]
[ 2 -2  5  3]
[19.0, 7.0, -4.0]
2026-11-27 14:00:00
Python NumPy floor on an array and a pandas Series, and pandas Timestamp floor to the hour
np.floor() returns floats; astype(int) converts them.

How does this work? np.floor() returned [ 2. -2. 5. 3.], still floats, so I called astype(int) to get integers. The Series floored to [19.0, 7.0, -4.0], and the timestamp floored down to 14:00:00.

For other array rounding modes, see how numpy.round() works in Python.

Example 8: Floor Decimal, Fraction and Large Numbers

Because floor() calls __floor__() on non-floats, it works with Decimal and Fraction values too:

import math
from decimal import Decimal
from fractions import Fraction

print(math.floor(Decimal("-2.5")))
print(math.floor(Fraction(7, 2)))
print(math.floor(True))
print(math.floor(1e20))

Output:

-3
3
1
100000000000000000000

How does this work? Decimal(“-2.5”) floored to -3 and Fraction(7, 2), which is 3.5, floored to 3. True counts as 1. 1e20 came back as the exact int 100000000000000000000, because large floats have no fractional part left.

If you need the fraction that floor() throws away, see how to get the decimal part of a number in Python.

Python floor vs round, int, ceil and //

People often mix up these rounding tools in Python. Here’s one loop that runs all of them on the same values:

import math

for x in (3.7, -3.7, 2.5):
    print(x, round(x), int(x), math.floor(x), math.ceil(x), x // 1)

Output:

3.7 4 3 3 4 3.0
-3.7 -4 -3 -4 -3 -4.0
2.5 2 2 2 3 2.0
Tool3.7-3.72.5Rule
round(x)4-42Nearest, ties to even
int(x)3-32Drops the decimals
math.floor(x)3-42Always down
math.ceil(x)4-33Always up
x // 13.0-4.02.0Down, keeps the float

Use floor() when you must never go over. Use round() for the closest value, explained in the Python round() function tutorial, and the Python ceil() function when you must always go up.

Common Mistakes with Python math.floor()

Here are the errors I see most, all from one real run:

import math

for value in ("3.7", None, float("inf"), float("nan"), 2 + 1j):
    try:
        math.floor(value)
    except Exception as err:
        print(type(err).__name__ + ":", err)

try:
    floor(2.5)
except NameError as err:
    print("NameError:", err)

Output:

TypeError: must be real number, not str
TypeError: must be real number, not NoneType
OverflowError: cannot convert float infinity to integer
ValueError: cannot convert float NaN to integer
TypeError: must be real number, not complex
NameError: name 'floor' is not defined