A café owner I'll call Maya started selling macarons for Rs 12.10 and biscotti for Rs 16.10. A customer bought one of each. The bill said:

Rs 28.200000000000003

The customer asked if she was being charged for atoms.

The prices are right. The adding is right. So where does that tiny extra come from?

Try it yourself
print(12.10 + 16.10) # 28.200000000000003
print(0.1 + 0.2) # 0.30000000000000004
print(0.1 + 0.2 == 0.3) # False

This isn't a Python bug. Almost every programming language does the same thing.

Why it happens

Computers store numbers in binary, using only 0s and 1s. Some decimals can't be written exactly in binary, the same way 1/3 can't be written exactly in decimal (0.3333… forever).

0.1 is one of those numbers. Python stores the closest value it can, which is off by a tiny amount. Most of the time you never see it. But add a few of these together and the tiny error shows up in the last digits.

Why it matters

For a game score, nobody cares. For money, it matters:

a bill showing 14 digits after the point looks broken
checking if total == 28.2: can fail when you expect it to pass
tiny errors add up across thousands of bills
How real developers handle it

  1. Count in paise, not rupees. Store whole numbers and only divide when showing the bill:

macaron = 1210 # paise
biscotti = 1610
total = macaron + biscotti
print(total / 100) # 28.2

  1. Use the decimal module when every paisa must be exact:

from decimal import Decimal
print(Decimal("12.10") + Decimal("16.10")) # 28.20

Pass the prices as text ("12.10"), not as numbers, or the error is already baked in.

  1. For showing a bill, Python has a small built-in tool that keeps only 2 digits after the point. It's the one beginners need first.

Want to fix Maya's bill yourself?

This exact bug is Mission 5 on Tryline, a free Python course where you learn by fixing bugs for a small café, right in your browser. One short lesson teaches the tool, then you fix the real code.

https://tryline.dev/?from=devto