Why quoting software should count in pence
Binary floating point cannot hold a tidy decimal exactly. Charming in a tutorial, less so on a 4,000-line quote. A short defence of integer money.
Type 0.1 + 0.2 into a JavaScript console and it answers 0.30000000000000004. Charming in a tutorial. Less charming on a 4,000-line quote, where the same arithmetic runs thousands of times before anyone reads the total.
Why floats cannot hold money
Computers store ordinary decimal numbers in binary floating point, and most tidy decimals have no exact binary form. One tenth in binary is a repeating fraction, the way one third is in decimal, so the machine stores the nearest value it can represent and the error tags along through every subsequent operation. The classics are worth seeing once.
| You write | You get |
|---|---|
| 1.03 - 0.42 | 0.6100000000000001 |
| 0.1 * 3 | 0.30000000000000004 |
| 1.005 rounded to 2 places | 1.00, because the stored value sits fractionally below 1.005 |
Integers and minor units
The boring, correct answer has been industry practice in payments for decades: store money as a whole number of the currency's minor unit, alongside the currency code. £149 is 14900 pence. Integer addition and subtraction are exact, comparisons are exact, and a column of a million line totals sums to the same value every time on every machine. The currency travels with the amount because the minor unit is not always two digits: yen has none and Bahraini dinar has three, so an amount without its currency is not even interpretable.
Round once, at the edge
Calculation still needs fractions. A unit price times a quantity times a discount percentage produces values finer than a penny, and that is fine, as long as the fractions live in decimal arithmetic during the calculation and rounding happens once, at the final display. Round in the middle and the order you round in becomes part of the price.
| Policy | Working | Total |
|---|---|---|
| Round each line, then sum | £11.00 + £11.00 + £11.00 | £33.00 |
| Sum, then round once | £32.985 rounded half-up | £32.99 |
Neither policy is wrong in the abstract. What is wrong is not choosing, because the moment your quote and your customer's ERP round in different places, a penny of disagreement appears on its own line in somebody's reconciliation report and an email thread starts that costs more than the penny ever did. Pick one rule, write it down and apply it everywhere.
A quote is a promise about a number. The arithmetic behind it should be the most trustworthy thing in the building, which is why the first engineering decision in any quoting system worth the name is also the least glamorous one: count in pence.