Adding and Subtracting Decimals
Adding and subtracting decimals works just like whole numbers once you line up the decimal points. This lesson shows the column method, borrowing across the point, estimating to catch mistakes, and why 0.1 + 0.2 is not 0.3 with floats but is with Decimal.
Line up the points
Write the numbers in columns with the decimal points directly above each other, pad with zeros so every number has the same number of places, then add as usual and keep the point in the same column:
12.40
+ 3.75
+ 0.08
───────
16.23Lining up the points makes sure you add tenths to tenths and hundredths to hundredths, the same reason fractions need a common denominator. Padding with zeros is that common denominator: 12.4 becomes .
Subtracting and borrowing
Borrowing works across the decimal point exactly like between any other two places:
20.00 (pad 20 to 20.00)
− 7.36
───────
12.64Check by adding back: . Subtraction answers are easy to check this way, so do it.
Estimate first
Round each number to a convenient whole number before calculating. If the exact answer is far from the estimate, the points were probably misaligned:
| Problem | Estimate | Exact |
|---|---|---|
| 16.23 | ||
| 12.64 | ||
| 1.937 | ||
| 5.35 |
Floats get it (slightly) wrong
Computers usually store decimals as binary floating-point numbers. Most tenths and hundredths have no exact binary form (just as has no exact decimal form), so tiny errors appear:
print(0.1 + 0.2)
print(0.1 + 0.2 == 0.3)
print(20 - 7.36)
print(round(0.1 + 0.2, 10) == 0.3)0.30000000000000004
False
12.64
True
Sometimes the error is hidden by printing ( shows as 12.64), sometimes it shows. Comparing floats with == is the real danger. See Floating Point for the full story.
Decimal gets it right
Decimal does column arithmetic in base 10, exactly like you do on paper. It also keeps the number of places: adding numbers with 2 decimal places gives a result with 2 places.
from decimal import Decimal as D
print(D("0.1") + D("0.2"))
print(D("0.1") + D("0.2") == D("0.3"))
print(D("12.40") + D("3.75") + D("0.08"))
print(D("20") - D("7.36"))
print(D("1.50") + D("2.5")) # result keeps the most places0.3
True
16.23
12.64
4.00
A shopping cart
Here the float total drifts and the Decimal total does not:
from decimal import Decimal
prices = ["19.99", "4.99", "9.95", "0.07"]
float_total = 0.0
for p in prices:
float_total += float(p)
dec_total = sum(Decimal(p) for p in prices)
print("float total: ", float_total)
print("Decimal total:", dec_total)
print("float total == 35?", float_total == 35)
print("change from 50 (Decimal):", Decimal("50") - dec_total)float total: 34.99999999999999
Decimal total: 35.00
float total == 35? False
change from 50 (Decimal): 15.00
The four prices add up to exactly 35.00, but the float total comes out a hair below 35, so a check like total == 35 (or a free-shipping test total >= 35) gives the wrong answer. With Decimal the result is exactly 35.00.
Note:
sum()starts from the integer 0, and0 + Decimal(...)is fine. MixingDecimalwithfloat(Decimal("1.5") + 0.1) raises aTypeErroron purpose, so float errors cannot sneak in.
In software
- Accounting and payments: totals, balances and refunds are sums and differences of decimal amounts. Payment APIs such as Stripe avoid the problem entirely by sending amounts as integer cents (
1999for 19.99), and adding integers is always exact. - Timestamps and durations: adding many float seconds (0.016 per frame) accumulates error; game engines often count frames or milliseconds as integers.
- Spreadsheets show 0.30000000000000004 as 0.3 because they round for display, but the stored value is still off, which is why
=0.1+0.2=0.3can be FALSE.
Exercises
Exercise 1: Column addition
Compute .
Answer
23.055
Solution
7.080
+ 15.600
+ 0.375
────────
23.055Estimate: , close to 23.055.
Exercise 2: Borrowing across the point
Compute and check by adding back.
Answer
31.53
Solution
50.00
− 18.47
───────
31.53 check: 31.53 + 18.47 = 50.00Exercise 3: Change from a note
A coffee costs 3.85 and a muffin 2.70. You pay with a 10 note. How much change do you get?
Answer
3.45
Solution
Exercise 4: Balance a ledger
A wallet starts at 250.00. Transactions: −19.99, −0.01, +100.10, −30.30, −0.20. Compute the balance with floats and with Decimal, and test whether each equals 299.60.
Answer
The exact balance is 299.60. Both tests pass here, but only the Decimal one is guaranteed to.
Solution
; ; ; .
from decimal import Decimal
moves = ["-19.99", "-0.01", "+100.10", "-30.30", "-0.20"]
f = 250.0
for m in moves:
f += float(m)
d = Decimal("250.00") + sum(Decimal(m) for m in moves)
print("float: ", f, f == 299.60)
print("Decimal:", d, d == Decimal("299.60"))float: 299.6 True
Decimal: 299.60 True
This time the float errors happened to cancel out. With other amounts (like the shopping cart above) they don't, and you cannot tell in advance which case you are in. Decimal is exact every time.
Test yourself
What is ?
- 5.25
- 1.20
- 4.125
- 5.20
What is ?
- 7.32
- 6.32
- 7.68
- 6.42
What does print(Decimal("0.1") + Decimal("0.2") == Decimal("0.3")) show?
- False
- 0.3
- True
- TypeError