Rounding and Repeating Decimals
Some decimals stop, others repeat a block of digits forever, and both often need rounding to a sensible number of places. This lesson rounds to a chosen place, explains the different tie-breaking rules (including the one Python's round() uses), and shows how to find the repeating block of any fraction and turn a repeating decimal back into a fraction.
Rounding to a decimal place
To round to a given place, look at the next digit to the right:
- 0–4: round down (keep the digit, drop the rest).
- 5–9: round up (add 1 to the digit, drop the rest).
| Number | To 1 place | To 2 places | To whole number |
|---|---|---|---|
| 3.14159 | 3.1 | 3.14 | 3 |
| 2.678 | 2.7 | 2.68 | 3 |
| 0.0449 | 0.0 | 0.04 | 0 |
| 9.995 | 10.0 | 10.00 (tie, rounded up) | 10 |
Look only at the one next digit. Rounding 0.0449 to two places looks at the third digit (4), so the answer is 0.04, even though 0.045 would round up. Rounding twice () gives a different, wrong answer.
Ties: half up or half to even?
When the dropped part is exactly 5 (2.5, or 0.125 to two places), there are two common rules:
- Round half up (school rule): , , .
- Round half to even ("banker's rounding"): go to the neighbour whose last digit is even: , , .
Half to even avoids a bias: with half up, ties always go up, so totals of many rounded numbers creep upward. Python's built-in round() uses half to even:
print([round(x) for x in [0.5, 1.5, 2.5, 3.5, -2.5]])
print(round(2.675, 2)) # expect 2.68? the float is really 2.67499999...[0, 2, 2, 4, -2]
2.67
The second line is not banker's rounding at all: the float 2.675 is stored as 2.67499999999999982236431605997495353221893310546875, which is below the tie, so it rounds down. To round exactly as written, use Decimal and choose the rule explicitly:
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN, ROUND_DOWN, ROUND_CEILING
x = Decimal("2.675")
cent = Decimal("0.01")
for rule in [ROUND_HALF_UP, ROUND_HALF_EVEN, ROUND_DOWN, ROUND_CEILING]:
print(f"{rule:16} {x.quantize(cent, rounding=rule)}")
print(Decimal(2.675)) # what the float really holdsROUND_HALF_UP 2.68
ROUND_HALF_EVEN 2.68
ROUND_DOWN 2.67
ROUND_CEILING 2.68
2.67499999999999982236431605997495353221893310546875
Here half up and half even agree, because the 8 in 2.68 is already even. Try Decimal("2.665") and they differ (2.67 vs 2.66).
Repeating decimals
Dividing the numerator by the denominator either stops or falls into a loop. It must loop eventually: in long division by , the remainder is always one of , so after at most steps a remainder repeats, and from then on the digits repeat too.
The bracket (or a bar over the digits, in books) marks the repeating block. A fraction in lowest terms terminates exactly when its denominator has no prime factors other than 2 and 5.
This program does the long division and stops when a remainder comes back:
def expand(num, den):
whole, rem = divmod(num, den)
digits, seen = [], {}
while rem and rem not in seen:
seen[rem] = len(digits)
rem *= 10
digits.append(str(rem // den))
rem %= den
if not rem:
return f"{whole}." + "".join(digits) if digits else str(whole)
start = seen[rem]
return f"{whole}." + "".join(digits[:start]) + "(" + "".join(digits[start:]) + ")"
for n, d in [(1, 8), (1, 3), (3, 11), (1, 6), (1, 7), (5, 12), (22, 7)]:
print(f"{n}/{d} = {expand(n, d)}")1/8 = 0.125
1/3 = 0.(3)
3/11 = 0.(27)
1/6 = 0.1(6)
1/7 = 0.(142857)
5/12 = 0.41(6)
22/7 = 3.(142857)
From repeating decimal back to a fraction
Let The block has 2 digits, so multiply by and subtract:
The rule: a block of digits that starts right after the point equals that block over nines. , and , which is why equals exactly 1.
Note: When you round a repeating decimal (), you lose information;
Fraction(1, 3)keeps it.Fraction("0.33")is , not , andFraction(0.33).limit_denominator(100)can recover a simple fraction that is close.
In software
- Currency rounding rules are legal requirements, not style: some tax rules demand half up, many banking systems use half even, and some currencies round to 0.05. Always pass the rule explicitly (
quantize(…, rounding=…)). - Display vs storage: format for display (
f"{x:.2f}"), but keep the full value for further calculation, or the errors compound. - Ceiling division for pages or batches: 47 items at 10 per page need pages. In integers:
-(-47 // 10).
Exercises
Exercise 1: Round it
Round 18.0462 to one, two and three decimal places.
Answer
18.0, 18.05, 18.046
Solution
Exercise 2: Tie rules
Round 0.125 and 0.135 to two places with round half up and with round half to even.
Answer
Half up: 0.13 and 0.14. Half even: 0.12 and 0.14.
Solution
Both are exact ties. Half up always goes up. Half even picks the neighbour ending in an even digit: 0.12 (not 0.13) and 0.14 (not 0.13).
from decimal import Decimal as D, ROUND_HALF_UP, ROUND_HALF_EVEN
for x in ["0.125", "0.135"]:
q = D("0.01")
print(x, D(x).quantize(q, ROUND_HALF_UP), D(x).quantize(q, ROUND_HALF_EVEN))0.125 0.13 0.12
0.135 0.14 0.14
Exercise 3: Repeating block
Write and as decimals, marking the repeating part.
Answer
and
Solution
: remainder 4 every step, digit 4 forever. : ; ; ; remainder 4 repeats, so the 3 repeats: 0.58333…
Exercise 4: Back to a fraction
Write and as fractions in lowest terms, and check with Fraction.
Answer
and
Solution
from fractions import Fraction as F
print(F(36, 99), F(15, 90))
print(float(F(4, 11)), float(F(1, 6)))4/11 1/6
0.36363636363636365 0.16666666666666666
Test yourself
What does Python's round(4.5) return?
- 5
- 4
- 4.5
- Error
Which fraction has a repeating decimal?
What is as a fraction in lowest terms?