A 10% discount, three VAT rates, one cent off: the rounding rules behind a quote app
A painter in Lisbon writes a quote on their phone at the end of a site visit: 42 m² of walls, two coats, materials, a 10% discount because the client is a neighbour. The client gets a link, opens it on their own phone an
A painter in Lisbon writes a quote on their phone at the end of a site visit: 42 m² of walls, two coats, materials, a 10% discount because the client is a neighbour. The client gets a link, opens it on their own phone and taps "Accept".
That is the whole product. The hard part is that the numbers on the client's screen, on the PDF and in the painter's records must be the same to the cent, in nine countries with different currencies and tax rules. Here is what we learned building the calculation core of RivoBuild.
1. Money is an integer. Always.
Every amount is stored as an integer in the currency's minor unit: cents for EUR, whole pesos for CLP. No floats anywhere near a total.
// 12,34 € is stored as 1234
formatMoney(1234, 'EUR', 'pt-PT'); // "12,34 €"
formatMoney(15000, 'CLP', 'es-CL'); // "$15.000"
The first surprise was how many decimals a currency has. We assumed Intl.NumberFormat was the source of truth. It isn't, for storage: CLDR (what Intl uses) shows the Colombian peso with 0 decimals, while ISO 4217 says 2. If your database follows one and your formatter follows the other, a 1 000 000 COP quote becomes 10 000 COP on screen.
We picked ISO 4217 for storage, kept a small explicit table of exceptions (0, 3 and 4-decimal currencies), and made the formatter refuse anything that isn't a safe integer:
if (!Number.isSafeInteger(amountMinor)) {
throw new RangeError(`Money must be a safe integer in minor units, got ${amountMinor}`);
}
The currency comes from the company's country, never from the UI language. A Chilean company can use the app in Portuguese and still quote in CLP.
2. Round each line once, half up
quantity × unit price is rounded to the minor unit per line, half up. Quantities can have decimals (12.5 m²), prices are integers, so every intermediate value is a bigint and we divide once at the end.
Rounding per line (not on the total) matters because the client sees the lines. If the PDF shows 12,5 × 7,99 € = 99,88 €, the total must be the sum of what the client can read.
3. A global discount across three tax rates
Here is the one that cost us a day. In Portugal one quote can mix 23%, 13% and 6% VAT lines. The professional types "10% off the total". Which VAT does the discount reduce?
The answer is: all of them, in proportion. We split the discount across priced lines by their amount, using the largest remainder method so the parts add up exactly to the discount — no cent lost, no cent invented:
// 1000 cents discount over lines of 3333, 3333 and 3334
allocateProportionally(1000, [3333, 3333, 3334]); // [333, 333, 334]
Naive Math.round on each share would give [333, 333, 333] and a discount of 9,99 € instead of 10,00 €. Clients notice.
4. Tax is computed per rate group, rounded once
After the discount, lines are grouped by tax rate and the tax is computed once per group on the discounted base. Not per line, not on the grand total. That is how invoices are usually read by accountants, and it keeps the PDF's tax table consistent with the totals above it.
Withholding (retention) works the same way but on the whole base and with a negative sign, so total = net + taxes − withholding stays a plain sum.
5. Tax rates are data, not code
There is no if (country === 'PT') in the calculation. Each rate is a row: country, kind (VAT, withholding…), basis points, the mention printed on the PDF, an internal source note, and a withdrawnAt date instead of deletion — quotes already sent must keep the rate they were made with.
Some real-world rules fit this shape better than expected. One country taxes building work with materials at 18% on 10% of the contract value: that is simply a rate of 1.8% (180 bps) with a clear mention. Others don't fit at all — a cost-plus model where VAT applies only to the contractor's profit needs its own calculation path, and we are building it as a separate, fully tested mode instead of bending the generic one.
Two rules we keep no matter what:
- The app never cites laws or articles to the user. It shows plain names ("Reduced VAT rate") and a note to confirm with an accountant. The professional decides.
- Calculation code lives in pure TypeScript (
lib/core), with no framework imports, and has 100% test coverage. It is shared by the server, the web app and, later, a native app.
6. What the client sees is a snapshot
When a quote is sent, we freeze a snapshot of everything the client can see (lines, totals, taxes, company details) and hash it. The approval link shows that snapshot; costs and margins are never in it. If the professional edits the quote, a new version is sent — the client never approves numbers that changed under them.
A test checks that the client payload contains no cost or margin. Fun fact: it once failed because the content hash happened to contain the digits we used as a canary cost. We now exclude the hash before matching.
Takeaways
- Integers in minor units, ISO 4217 for decimals, and an explicit exception table.
- Round per line, allocate discounts with largest remainder, tax per rate group.
- Rates as data with history; odd rules either fit as data or get their own tested path.
- Snapshot what the client approves.
If you build anything that prints money for someone else to sign, I hope this saves you the cent we spent a day chasing.
RivoBuild is a quote app for construction professionals: you write the quote on your phone and the client approves it by link. Public overview: github.com/Rapid-Dreams/rivobuild-public.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.