Sub topic 3
Rounding numbers: floor, ceil, trunc, and round
This chapter assumes you can call functions and print values in Python, and that you have used import math before. Python has four common ways to turn a float into a whole number or a shorter float. They differ most on negative numbers and on halves.
The three directional functions
math.floor, math.ceil, and math.trunc each return an int.
math.floor(x)returns the largest integer less than or equal tox. It rounds toward negative infinity.math.ceil(x)returns the smallest integer greater than or equal tox. It rounds toward positive infinity.math.trunc(x)drops the fractional part. It rounds toward zero.
import math
for x in (2.7, -2.7):
print(x, math.floor(x), math.ceil(x), math.trunc(x), round(x))
# 2.7 2 3 2 3
# -2.7 -3 -2 -2 -3| Function | 2.7 | -2.7 | Direction |
|---|---|---|---|
| `math.floor` | 2 | -3 | toward negative infinity |
| `math.ceil` | 3 | -2 | toward positive infinity |
| `math.trunc` | 2 | -2 | toward zero |
| `round` | 3 | -3 | to the nearest value |
For positive numbers, floor and trunc agree. They differ only for negatives. The built-in int(-2.7) also truncates, so it gives -2, not -3.
The built-in round
round is a built-in, so it needs no import. Its result type depends on the arguments.
print(round(3.14159, 2)) # 3.14
print(round(7.8)) # 8 (an int)
print(round(7.8, 0)) # 8.0 (a float)
print(round(1234, -2)) # 1200With no second argument, round returns an int. With ndigits given, it returns the same type as the input. A negative ndigits rounds to tens, hundreds, and so on.
Common error: halves round to even
Many people expect 2.5 to round to 3. Python uses banker's rounding, also called round half to even. A value exactly halfway between two integers goes to the even one.
print(round(0.5)) # 0
print(round(1.5)) # 2
print(round(2.5)) # 2
print(round(3.5)) # 4This avoids a systematic upward bias when you round many values. It can surprise you if you expect schoolbook rounding.
Common error: the stored value is not the typed value
Floats are stored in binary, so a literal such as 2.675 is really slightly below 2.675. round works on the stored value.
print(round(2.675, 2)) # 2.67, not 2.68This is not a bug in round. If you need decimal rounding rules, for example for money, use the decimal module and name the rounding mode.
from decimal import Decimal, ROUND_HALF_UP
price = Decimal("2.675")
print(price.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)) # 2.68Build the Decimal from a string. Decimal(2.675) would copy the float's binary error into the result.
Choosing a function
- Use
floorfor things like "which whole page does item 17.5 fall on", where you always step down. - Use
ceilwhen a partial unit still counts as a full one, such as the number of boxes needed for 25 items at 10 per box:math.ceil(25 / 10)is 3. - Use
truncwhen you want to discard the fractional part regardless of sign. - Use
roundfor display or approximate values, and remember the half-to-even rule.
Exercise
Predict the output of print(math.floor(-0.5), math.ceil(-0.5), math.trunc(-0.5), round(-0.5)). Then explain why round(-0.5) does not give -1.
Show answer
It prints "-1 0 0 0". Floor goes toward negative infinity, giving -1. Ceil gives 0 and trunc gives 0, because -0.5 has no integer part. round(-0.5) is exactly halfway between -1 and 0, so round half to even picks 0, the even neighbour. Since the result is an int, it prints as 0, not -0.