TExcelTAD
Home › Basic Math & Pre-Algebra › Adding and Subtracting Decimals

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:

text
   12.40
 +  3.75
 +  0.08
 ───────
   16.23

Lining 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 12.40=124010012.40 = \frac{1240}{100}.

Subtracting and borrowing

Borrowing works across the decimal point exactly like between any other two places:

text
   20.00          (pad 20 to 20.00)
 −  7.36
 ───────
   12.64

Check by adding back: 12.64+7.36=20.0012.64 + 7.36 = 20.00. 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
12.4+3.75+0.0812.4 + 3.75 + 0.08 12+4+0=1612 + 4 + 0 = 16 16.23
20−7.3620 - 7.36 20−7=1320 - 7 = 13 12.64
0.95+0.9870.95 + 0.987 1+1=21 + 1 = 2 1.937
105.2−99.85105.2 - 99.85 105−100=5105 - 100 = 5 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 13\frac{1}{3} has no exact decimal form), so tiny errors appear:

python · try it yourself
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 (20−7.3620 - 7.36 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.

python · try it yourself
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 places
0.3
True
16.23
12.64
4.00

A shopping cart

Here the float total drifts and the Decimal total does not:

python · try it yourself
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, and 0 + Decimal(...) is fine. Mixing Decimal with float (Decimal("1.5") + 0.1) raises a TypeError on 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 (1999 for 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.3 can be FALSE.

Exercises

Exercise 1: Column addition

Compute 7.08+15.6+0.3757.08 + 15.6 + 0.375.

Answer

23.055

Solution
text
    7.080
 + 15.600
 +  0.375
 ────────
   23.055

Estimate: 7+16+0=237 + 16 + 0 = 23, close to 23.055.

Exercise 2: Borrowing across the point

Compute 50−18.4750 - 18.47 and check by adding back.

Answer

31.53

Solution
text
   50.00
 − 18.47
 ───────
   31.53        check: 31.53 + 18.47 = 50.00

Exercise 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
3.85+2.70=6.5510.00−6.55=3.45\begin{aligned} 3.85 + 2.70 &= 6.55 \\ 10.00 - 6.55 &= 3.45 \end{aligned}

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

250−19.99−0.01=230250 - 19.99 - 0.01 = 230; +100.10=330.10+100.10 = 330.10; −30.30=299.80-30.30 = 299.80; −0.20=299.60-0.20 = 299.60.

python · try it yourself
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 4.5+0.754.5 + 0.75?

  1. 5.25
  2. 1.20
  3. 4.125
  4. 5.20

What is 9−2.689 - 2.68?

  1. 7.32
  2. 6.32
  3. 7.68
  4. 6.42

What does print(Decimal("0.1") + Decimal("0.2") == Decimal("0.3")) show?

  1. False
  2. 0.3
  3. True
  4. TypeError