Lesson 8 of 20 · 52 min · UG / professional
Decimal, money and the float trap
0.1 + 0.2 is a representation error, not a cashier total. decimal.Decimal or integer paise. This is the same topic as the language tutorial’s float chapter.
Pocket-money pouch
Combine skills into a small tool.
You will be able to
- ✓ Keep rupees as integer paise until a report needs a display string
- ✓ Treat 0.1 + 0.2 as a representation error, not a UPI total
- ✓ Know decimal.Decimal is the stdlib type when you must stay in rupees-with-fraction
The till is not a science experiment
0.1 + 0.2 is not always what a cashier expects. Binary floats are for DHT Celsius. UPI settlements and GST belong in integer paise, or in decimal.Decimal on a laptop, until a report needs a display string. Do not round inside the only GST function and also in the CSV writer — one policy, one place.
paise = rupees * 100, integer add, then format at the edge. Decimal("0.18") is the stdlib type when you must stay in rupees-with-fraction. Mixing float into a 40,000-row running total is how a one-paise drift becomes an audit finding.
Limits are real
Floats have magnitude and precision limits. A huge intern stipend still fits in Python int (unbounded). A mean temperature can be float. A GST total must not silently pick the wrong representation. This lesson is that split, not a list of IEEE trivia.
Words that matter
- Paise
- Integer money — 236 rupees is 23600 paise.
- Decimal
- Stdlib base-10 type for money-like figures.
- Representation error
- 0.1 + 0.2 in binary float is not a cashier total.
Common mistakes
Avoid: Using float for a running GST total across 40,000 UPI rows.
Do this: Integer paise or Decimal; format once at the end.
Run it step by step
Each box is a real program. Press Run, change a number, Run again — the output must follow your code.
1. Step 1 — paise, then display
200 rupees at 18 percent is 23600 paise. Divide at the edge.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
2. Step 2 — float is for DHT, not the till
Print the science add. Prefer 10 + 20 paise.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
On a full Python install — decimal
Rupee arithmetic that must match a ledger. Show the float trap next to Decimal.
No pip — stdlib.
Decimal for GST. Float for science only.
Real library code (not run in this browser sandbox)
from decimal import Decimal, ROUND_HALF_UP
GST = Decimal("0.18")
def add_gst(amount):
bill = (amount * (1 + GST)).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
return bill
print(add_gst(Decimal("200")))
print("float trap", 0.1 + 0.2)
Example program — UPI rows as paise
Integer add. INFO on the log line.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Your turn — GST in paise
add_gst_paise(paise) returns paise * 118 / 100. Print add_gst_paise(10000) so 11800 appears.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Self-assessment
Check your understanding before you mark the lesson complete.
Progress is stored in a browser cookie on this device.