How to Round Down in Python: Simple Guide with Examples
To round down in Python, use the
math.floor() function which returns the largest integer less than or equal to a given number. Import math and call math.floor(your_number) to get the rounded down value.Syntax
The math.floor() function rounds a number down to the nearest whole integer.
math.floor(x): Returns the largest integer less than or equal tox.- You must
import mathbefore using it.
python
import math result = math.floor(3.7) print(result)
Output
3
Example
This example shows how to round down different numbers using math.floor(). It works for positive and negative numbers.
python
import math numbers = [3.7, 5.9, -2.3, -7.8] rounded_down = [math.floor(num) for num in numbers] print(rounded_down)
Output
[3, 5, -3, -8]
Common Pitfalls
One common mistake is using the built-in round() function expecting it to always round down. round() rounds to the nearest integer, not always down.
Also, forgetting to import math causes errors.
python
import math # Wrong: round() does not always round down print(round(3.7)) # Output: 4 # Right: use math.floor() to always round down print(math.floor(3.7)) # Output: 3
Output
4
3
Quick Reference
| Function | Description | Example | Output |
|---|---|---|---|
| math.floor(x) | Rounds down to nearest integer | math.floor(4.9) | 4 |
| round(x) | Rounds to nearest integer (not always down) | round(4.9) | 5 |
Key Takeaways
Use math.floor() to round down numbers in Python.
Always import the math module before using math.floor().
round() does not always round down; it rounds to the nearest integer.
math.floor() works correctly with both positive and negative numbers.