Skip to content

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.

Tekniska anteckningarKabaido2 min läsning

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.

Standard double-precision floating point arithmetic, exactly as a JavaScript console reports it.
You writeYou get
1.03 - 0.420.6100000000000001
0.1 * 30.30000000000000004
1.005 rounded to 2 places1.00, because the stored value sits fractionally below 1.005
Binära flyttal0.1 + 0.2= 0.30000000000000004avvikelsen växer rad för radHeltal i pence10 + 20= 30, exaktavrunda halva uppåt en gång, vid visningenPengar lagras som hela minsta valutaenheter tillsammans med sin valuta, formateringen sker ytterst
The same sum in binary floats and in integer pence. One of these is exact every time.

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.

Three lines at £10.995 each, in exact decimal arithmetic, rounded half-up at different points.
PolicyWorkingTotal
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.

Källor och metod

Mer från bloggen

Tekniska anteckningar4 min läsning

The soft jaw order hiding in a chuck model

A soft jaw enquiry looks like two words and a chuck size. The serration, the jaw envelope and the material are all implied. How a seller reads one.

Läs inlägget
Tekniska anteckningar3 min läsning

Teaching software to read like a domain engineer

A general AI model knows what a milling cutter is. It does not know your milling cutter. What it takes to ground a model in a real industrial domain.

Läs inlägget
Inifrån arbetet2 min läsning

What an RFQ actually contains

An RFQ looks like a document. It is closer to a scattered set of claims, each hiding somewhere else. How engineers read one, and how software should.

Läs inlägget

Sätt kunskapen i arbete

Kabaido läser förfrågningar på samma sätt som ingenjörerna i de här inläggen: strukturerat, belagt med källa och ärligt om luckorna.