Python floor() Function Explained (math.floor, Examples, vs int & round)

Python floor() Function Explained (math.floor, Examples, vs int & round)

What is floor() in Python?

The floor() function in Python is used to round a number down to the nearest integer. It always returns the largest integer less than or equal to the given number.

This function is part of the built-in math module.

For example:

python
import math

print(math.floor(2.9))   # Output: 2
print(math.floor(-2.3))  # Output: -3

Notice that for negative numbers, the result moves further away from zero.

If you're new to Python math operations, you can also explore related functions in
Python numbers explained


Python floor() Syntax and Return Type

math.floor() Syntax Explained

The syntax of the floor() function is simple:

python
math.floor(x)
  • x → A numeric value (float or integer)
  • Returns → The largest integer ≤ x

Example:

python
import math

value = 5.7
result = math.floor(value)

print(result)  # Output: 5

What Does floor() Return in Python?

The floor() function always returns an integer type, even if the input is a float.

Example:

python
import math

result = math.floor(3.8)

print(result)        # 3
print(type(result))  # <class 'int'>

This makes it useful when you need clean integer values from floating-point calculations.


How to Use floor() in Python

Basic Example of math.floor()

Here is a simple example of flooring a decimal number:

python
import math

print(math.floor(10.99))  # Output: 10

This is commonly used in calculations where you want to discard decimal values safely.

Floor Positive vs Negative Numbers

One of the most important concepts (and commonly misunderstood):

python
import math

print(math.floor(4.2))   # 4
print(math.floor(-4.2))  # -5

Key takeaway:

  • Positive numbers → behave like truncation
  • Negative numbers → move to the next lower integer

This is where floor() differs from simple integer conversion.

Floor Float Values in Python

You can apply floor() directly to floating-point values:

python
import math

values = [1.1, 2.5, 3.9]

for v in values:
    print(math.floor(v))

Output:

text
1
2
3

Python floor() vs int() vs round()

Difference Between math.floor() and int()

The key difference between math.floor() and int() appears when working with negative numbers.

python
import math

print(math.floor(3.7))   # 3
print(int(3.7))          # 3

print(math.floor(-3.7))  # -4
print(int(-3.7))         # -3

Key difference:

  • math.floor() → Always rounds down
  • int() → Simply truncates (removes decimal part)

floor() vs ceil() vs round()

These three functions behave differently:

python
import math

print(math.floor(2.7))  # 2
print(math.ceil(2.7))   # 3
print(round(2.7))       # 3

For negative values:

python
print(math.floor(-2.7))  # -3
print(math.ceil(-2.7))   # -2
print(round(-2.7))       # -3

Summary:

  • floor() → round down
  • ceil() → round up
  • round() → nearest value

You can explore more rounding techniques Python ceil function explained

When to Use Each Method

  • Use floor() → when you need strict downward rounding
  • Use int() → when you just want to remove decimals
  • Use round() → when you want nearest integer
  • Use ceil() → when you need upper bound values

How to Floor a Number Without math.floor()

Using int() for Positive Numbers

For positive numbers, int() behaves like floor():

python
print(int(5.9))  # 5
WARNING
This does NOT work correctly for negative numbers.

Using Division Tricks (// Operator)

The floor division operator // can be used:

python
print(7 // 2)     # 3
print(-7 // 2)    # -4

// always performs floor division, even for negatives.

Custom Floor Function Example

You can create your own floor logic:

python
def custom_floor(x):
    if x >= 0:
        return int(x)
    else:
        return int(x) - 1 if x != int(x) else int(x)

print(custom_floor(3.7))   # 3
print(custom_floor(-3.7))  # -4

Real-World Use Cases of floor() in Python

Price Rounding (E-commerce Use Case)

Used to round prices down:

python
import math

price = 99.99
final_price = math.floor(price)

print(final_price)  # 99

Pagination Logic (Page Count Calculation)

Useful when calculating page numbers:

python
import math

total_items = 55
items_per_page = 10

pages = math.floor(total_items / items_per_page)

print(pages)  # 5

Helps determine full pages available

Time / Timestamp Rounding

Floor timestamps to nearest unit:

python
import math

seconds = 367.8
minutes = math.floor(seconds / 60)

print(minutes)  # 6

Data Cleaning in Pandas (floor values)

Used in data preprocessing:

python
import pandas as pd
import numpy as np

df = pd.DataFrame({'values': [1.9, 2.5, 3.8]})
df['floored'] = np.floor(df['values'])

print(df)

Common in:

  • data normalization
  • removing decimal noise

You can explore more at Python pandas tutorial


Python floor() with Pandas and NumPy

Using floor() in Pandas Series

In Pandas, you typically use NumPy’s floor() function to apply flooring on a column.

python
import pandas as pd
import numpy as np

df = pd.DataFrame({
    'values': [1.9, 2.5, 3.8, -2.3]
})

df['floored'] = np.floor(df['values'])

print(df)

Output:

text
   values  floored
0     1.9      1.0
1     2.5      2.0
2     3.8      3.0
3    -2.3     -3.0

Useful in:

  • data preprocessing
  • removing decimal noise
  • preparing clean integer datasets

NumPy floor() Example

NumPy provides a vectorized version of floor() which works efficiently on arrays:

python
import numpy as np

arr = np.array([1.2, 2.9, 3.5, -1.7])

result = np.floor(arr)

print(result)

Output:

text
[ 1.  2.  3. -2.]

Advantage:

  • Faster than loops
  • Ideal for large datasets

Common Errors and Fixes

NameError: floor not defined

This error occurs when you try to use floor() without importing the math module.

python
print(floor(2.5))  # ❌ Error

Fix:

python
import math

print(math.floor(2.5))  # ✅ Works

Why floor() Gives Unexpected Output for Negative Numbers

Many beginners assume math.floor(-2.3) will return -2, but that is incorrect.

python
import math

print(math.floor(-2.3))  # -3

Reason:

  • floor() returns the largest integer less than or equal to the given number
  • Since -2 is greater than -2.3, it cannot be the floor
  • -3 is the correct answer because it is the next integer below -2.3

Quick comparison:

python
import math

print(math.floor(-2.3))  # -3
print(int(-2.3))         # -2

Here:

  • math.floor() moves toward negative infinity
  • int() truncates toward zero

Import Issues (math module)

Another common mistake is forgetting to prefix with math.

python
import math

print(floor(3.4))  # ❌ Error

Correct usage:

python
import math

print(math.floor(3.4))  # ✅

Frequently Asked Questions

1. What does floor() do in Python?

The floor() function returns the largest integer less than or equal to a given number. It is commonly used to round down floating-point values.

2. What is the difference between floor() and int() in Python?

floor() always rounds down, while int() truncates the decimal part. This difference is noticeable for negative numbers.

3. How do I use math.floor() in Python?

You can use it by importing the math module and calling math.floor(number).

4. Why does floor() give unexpected results for negative numbers?

floor() follows mathematical rules and always moves toward negative infinity, not toward zero.

5. Can I use floor() without importing math?

No, you must import the math module or use alternative methods like // or NumPy floor().

Summary

The floor() function in Python is a simple yet powerful tool used to round numbers down to the nearest integer.

Key takeaways:

  • math.floor() always rounds toward negative infinity
  • It behaves differently from int() for negative numbers
  • It is useful in real-world scenarios like:
    • price rounding
    • pagination calculations
    • time conversions
    • data preprocessing
  • Alternatives like //, int(), and NumPy provide flexibility depending on use case

If you need precise downward rounding, always prefer math.floor() over other methods.

For deeper understanding of Python functions, check Python functions explained


Official Documentation

Bashir Alam

Bashir Alam

Data Analyst and Machine Learning Engineer

Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in OCR, text extraction, data preprocessing, and predictive models.