Pull up a chair, this one looks like a party trick but it is really about how your invoices end up one paisa off. Open any Python or JavaScript console and add 0.1 and 0.2. You get 0.30000000000000004. Ask whether that equals 0.3 and the answer is false. Nothing is broken. The computer is doing exactly what it was designed to do, and once you see why, you will stop trusting decimals in places where they were never trustworthy.

A third, written in decimal

Try writing one third as a decimal. You get 0.3333 and it never ends. You can stop anywhere, but whatever you write down is slightly wrong. Nobody is surprised by this, because ten is not divisible by three.

Computers count in twos, not tens. A float stores a number as a binary fraction: halves, quarters, eighths, sixteenths and so on. A value like 0.5 or 0.75 fits perfectly because it is built from powers of two. But 0.1 is one tenth, and one tenth in binary is a repeating pattern, just like one third in decimal. The machine has a fixed number of bits, so it cuts the pattern off and keeps the nearest value it can hold. For 0.1 that stored value is a hair above the real thing, roughly 0.1000000000000000055. Same story for 0.2. Add two slightly-off numbers and the tiny errors show up in the sum.

The common type here is the 64-bit float, called double in Java and C, and the only number type for most purposes in JavaScript. It carries about 15 to 17 significant decimal digits. That sounds generous, and for physics, graphics and most statistics it is. Errors of one part in a quadrillion do not matter when you are computing the trajectory of a ball.

Where it starts to bite

Money is where people get hurt, because money has a rule that physics does not: the totals must match exactly. A customer in Kochi is billed ₹499.50 and the payment gateway says ₹499.50 was received. If your code holds the amount as a float, an equality check between the two can fail, and now a reconciliation report shows a mismatch nobody can explain.

The second trap is comparing floats with an equality check at all. Whether two computed values come out identical depends on the order you added them in. Sum a list from the front and from the back and you can land on two different last digits. If you must compare floats, compare with a small tolerance, and decide that tolerance on purpose.

The third is size. Whole numbers are exact in a double only up to 2 to the power of 53, which is 9,007,199,254,740,992. Beyond that, adding one to a number can do nothing at all. Large database IDs passed through JavaScript have been silently corrupted this way, which is why many APIs send big IDs as strings.

Store paise, not rupees

The oldest fix is also the simplest. Do not store ₹499.50. Store 49950 paise as an integer. Integers are exact. Adding, subtracting and comparing them never surprises you. You convert to rupees only when you print the value on a screen or a PDF. Many payment APIs already work this way; amounts are sent in the smallest currency unit for exactly this reason.

The other option is a decimal type, such as BigDecimal in Java, decimal in C# and Python’s Decimal, or NUMERIC in SQL databases. These store digits in base ten, so 0.1 really is 0.1. They are slower than floats, but nobody ever lost a customer over a billing system that was a few microseconds slower. Whichever you choose, the column in your database should match. A float column quietly undoes the care you took in the application.

One warning on decimals: build them from strings or integers, not from a float. Passing the float 0.1 into a decimal constructor in many languages faithfully copies the long, slightly wrong value. You will have the right type holding the wrong number.

Integers do not remove every problem

Here is the bit people skip. Storing paise fixes representation, but percentages still produce fractions of a paisa, so you must decide how to round. Take GST at 18% on an item priced at ₹99.99, which is 9999 paise. The tax is 1799.82 paise. You cannot charge 0.82 of a paisa.

Now suppose the invoice has three such lines. Round each line and you collect 1800 paise three times, which is 5400. Add the three prices first (29997 paise) and apply tax once, and you get 5399.46, which rounds to 5399. The two methods differ by a paisa, and both are defensible. Which one is correct depends on your tax rules and what your accountant has agreed to. Pick one, write it down, and make every system in the company use the same one. Also decide what rounding mode you use: half-up is what most people expect, while some libraries default to banker’s rounding, where exact halves go to the nearest even digit.

A rule of thumb you can carry

Ask what kind of quantity it is. If it is measured (temperature, distance, a model score), floats are the right tool and small errors are part of life. If it is counted or agreed (money, quantities in stock, number of seats), use integers or decimals and make the rounding rule explicit. And if two values need to match exactly across systems, never let a float sit between them.

Figures and names in the examples are illustrative.

Leave a Reply

Trending