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.

Technické poznámkyKabaido2 min čtení

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ární čísla s pohyblivou řádovou čárkou0.1 + 0.2= 0.30000000000000004odchylka se sčítá položku po položceCeločíselné pence10 + 20= přesně 30zaokrouhlit půlku nahoru jednou, až při zobrazeníPeníze se ukládají jako celé nejmenší jednotky spolu s měnou; formátuje se až na okraji
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.

Zdroje a metodika

Další z blogu

Technické poznámky4 min čtení

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.

Přečíst článek
Technické poznámky3 min čtení

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.

Přečíst článek
Pohled do praxe2 min čtení

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.

Přečíst článek

Využijte tyto poznatky v praxi

Kabaido čte požadavky stejně jako technici v těchto článcích: strukturovaně, s doloženými zdroji a s poctivým přiznáním mezer.