When you code money, count the pennies, not the dollars
Computers fumble 10 cents plus 20 cents, so programmers keep everything as whole cents until the very end.

Computerphile · Floating Point Numbers - ComputerphileSooner or later, every new programmer tries typing 0.1 + 0.2 into code, expecting the tidy 0.3 from a school calculator. Instead the computer answers with something like 0.3000000001. That strange extra 1 is the introduction to floating point numbers, which are essentially scientific notation for computers.
For a lot of work, that stray bit at the end does not matter. With money, it does, because people notice. Tell a computer to add 10p, or 0.1 pounds, to 20p, and it comes back with an answer that is visibly off, and suddenly the error is obvious to anyone checking the change.
The workaround is deliberately low tech. Forget pounds and dollars while you calculate, and count everything as whole pence or cents. A coffee priced at $2.35 becomes 235, and a sandwich at $4.10 becomes 410. Add and subtract those whole numbers, and only at the very last step, when you need to show a human a total, divide by 100.
Some more advanced programming languages also offer a built-in option called a decimal type that is designed for this exact problem. For everyday code, the integer habit means converting every price to pennies on the way in and keeping it that way until the display step.
That one change keeps the till exactly what customers expect to see.
Questions people ask
Why can't a computer just add 0.1 and 0.2 correctly?
It stores decimals in binary, using halves, quarters, eighths, and so on, rather than tenths and hundredths. In binary, 0.1 is a repeating fraction that goes on forever, so the computer has to cut it off after a fixed number of digits. Those tiny cutoffs add up to 0.3000000001.
What is a decimal type?
It is a special number format offered by some programming languages that stores decimals the way humans write them, in base 10, instead of in binary. It is slower and uses more memory than standard floating point numbers, but it keeps money math exact. For simple apps, many programmers still prefer the integer pennies trick because it works in any language.
Does this rounding error matter outside money?
Usually not. A video game does not care if a graphic is off by a hundredthousandth of a pixel, and most science and engineering work has real world tolerances larger than the error. Money is special because people expect exact cents, and even one extra fraction of a penny looks wrong on a bill.