Open Python or your browser's console and type 0.1 + 0.2. The answer is 0.30000000000000004. Strange, but it is not a bug in Python, and JavaScript gives exactly the same result. An interviewer can turn this into a real question: "In what data type would you store a price?"
Why 0.1 isn't really 0.1
Python's float and JavaScript's numbers are IEEE 754 double-precision values, stored in binary. In base 2, one tenth is a fraction that never ends: 0.0001100110011…. The Python documentation compares it to writing 1/3 in decimal: 0.3, 0.33, 0.333, and it is never exactly 1/3. So the computer keeps the closest value it can. The 0.1 you typed is really stored as 0.1000000000000000055511151231257827021181583404541015625.
The error is tiny and always the same; nothing is random. But it means equality checks like 0.1 + 0.2 == 0.3 come out False, and, as Python's decimal docs put it, "differences can accumulate" over many operations.
How money is stored instead
- Whole paise. Keep ₹10.50 as the integer
1050. Integers are exact (as long as they fit their type, which is plenty for money). Payment gateways already work this way: Razorpay asks for the "amount in the smallest currency sub-unit", so ₹299 is passed as29900, and Stripe takes "the smallest currency unit" too. - DECIMAL / NUMERIC. Databases have exact decimal types; PostgreSQL recommends
numeric"for storing monetary amounts and other quantities where exactness is required". In Python, useDecimal, built from a string such asDecimal("10.50"), not from a float.
One classic trap when converting: int(price * 100) can drop a paisa, because price * 100 may land a hair below the whole number. Round, or convert from the string with Decimal.
None of this makes floats bad. They're the right tool for physics, graphics or machine learning. The rule is only for money, where every paisa has to add up exactly.
The answer to give
Never a float: whole paise, or DECIMAL.
In the interview: "I'd never store money as a binary float. I'd keep it as an integer in the smallest unit, paise, or in a DECIMAL column, convert at the edges, and use an explicit rounding rule when splitting amounts."
Sources: Python tutorial, Floating-Point Arithmetic; MDN, Number.EPSILON; Python decimal module; Razorpay integration docs; Stripe PaymentIntents; PostgreSQL numeric types.