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:
import math
print(math.floor(2.9)) # Output: 2
print(math.floor(-2.3)) # Output: -3Notice 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:
math.floor(x)x→ A numeric value (float or integer)- Returns → The largest integer ≤
x
Example:
import math
value = 5.7
result = math.floor(value)
print(result) # Output: 5What Does floor() Return in Python?
The floor() function always returns an integer type, even if the input is a float.
Example:
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:
import math
print(math.floor(10.99)) # Output: 10This 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):
import math
print(math.floor(4.2)) # 4
print(math.floor(-4.2)) # -5Key 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:
import math
values = [1.1, 2.5, 3.9]
for v in values:
print(math.floor(v))Output:
1
2
3Python 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.
import math
print(math.floor(3.7)) # 3
print(int(3.7)) # 3
print(math.floor(-3.7)) # -4
print(int(-3.7)) # -3Key difference:
math.floor()→ Always rounds downint()→ Simply truncates (removes decimal part)
floor() vs ceil() vs round()
These three functions behave differently:
import math
print(math.floor(2.7)) # 2
print(math.ceil(2.7)) # 3
print(round(2.7)) # 3For negative values:
print(math.floor(-2.7)) # -3
print(math.ceil(-2.7)) # -2
print(round(-2.7)) # -3Summary:
floor()→ round downceil()→ round upround()→ 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():
print(int(5.9)) # 5Using Division Tricks (// Operator)
The floor division operator // can be used:
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:
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)) # -4Real-World Use Cases of floor() in Python
Price Rounding (E-commerce Use Case)
Used to round prices down:
import math
price = 99.99
final_price = math.floor(price)
print(final_price) # 99Pagination Logic (Page Count Calculation)
Useful when calculating page numbers:
import math
total_items = 55
items_per_page = 10
pages = math.floor(total_items / items_per_page)
print(pages) # 5Helps determine full pages available
Time / Timestamp Rounding
Floor timestamps to nearest unit:
import math
seconds = 367.8
minutes = math.floor(seconds / 60)
print(minutes) # 6Data Cleaning in Pandas (floor values)
Used in data preprocessing:
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.
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:
values floored
0 1.9 1.0
1 2.5 2.0
2 3.8 3.0
3 -2.3 -3.0Useful 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:
import numpy as np
arr = np.array([1.2, 2.9, 3.5, -1.7])
result = np.floor(arr)
print(result)Output:
[ 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.
print(floor(2.5)) # ❌ ErrorFix:
import math
print(math.floor(2.5)) # ✅ WorksWhy floor() Gives Unexpected Output for Negative Numbers
Many beginners assume math.floor(-2.3) will return -2, but that is incorrect.
import math
print(math.floor(-2.3)) # -3Reason:
floor()returns the largest integer less than or equal to the given number- Since
-2is greater than-2.3, it cannot be the floor -3is the correct answer because it is the next integer below-2.3
Quick comparison:
import math
print(math.floor(-2.3)) # -3
print(int(-2.3)) # -2Here:
math.floor()moves toward negative infinityint()truncates toward zero
Import Issues (math module)
Another common mistake is forgetting to prefix with math.
import math
print(floor(3.4)) # ❌ ErrorCorrect usage:
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










![Pandas Pivot Simplified [In-Depth Tutorial]](/pandas-pivot/python-pandas-pivot_hu_dc49d2c8eb5c965b.webp)