A blue cash organiser, coin stacks and a cream calculator on a bright desk.

· Engineering · 5 min read

Money in software: whole cents and VAT codes

Why amounts belong in whole cents, why a VAT rate alone does not describe a sale, and how rounding per line quietly changes an invoice.

Software that handles money has to get three things right before it gets anything else right: how an amount is stored, how it is rounded, and what a VAT line actually means. This article walks through each, with the rules my own administration tool, Finance OS, settled on and the Dutch tax rules they follow. It is written for developers who are about to build anything with an invoice in it.

A float cannot hold a cent

JavaScript numbers are binary floating point. Most decimal fractions have no exact binary form, so they are stored as the nearest value that does. You can see it in any console:

0.1 + 0.2;                 // 0.30000000000000004
1.005 * 100;               // 100.49999999999999
Math.round(1.005 * 100);   // 100, not 101

The second line is the dangerous one. €1.005 should round to €1.01. It rounds to €1.00, because the number was never 1.005 to begin with. Nothing crashes and no test fails unless you wrote one for exactly that value. The invoice is simply one cent off.

Whole cents and basis points

The fix is old and boring: never store a fraction. An amount is a whole number of cents, and a percentage is a whole number of basis points, so 21% is 2100 and 9% is 900. Payment providers work the same way. Stripe, for example, expects every amount in the currency's minor unit, so ten euros is sent as 1000.

Integers in JavaScript are exact up to Number.MAX_SAFE_INTEGER, which is 9,007,199,254,740,991. In cents that is about ninety trillion euros, so an ordinary administration will not run out.

One caution if you ever leave the euro: not every currency has two decimals. Some have none and a few have three. Store the minor unit of the currency you are in, not "cents" as an assumption.

Which way does a half go?

With whole cents, rounding only happens in one place: when a percentage is applied. The Dutch tax office describes the rule for the VAT amount on an invoice as arithmetic rounding to whole cents: €20.124 becomes €20.12 and €20.125 becomes €20.13.

That sounds like Math.round, and for positive amounts it is. For negative amounts it is not. JavaScript rounds a half towards positive infinity, so Math.round(-2.5) is -2. A credit note is a negative invoice, and it has to mirror the original exactly:

// VAT at 9% on 1.50 is 13.5 cents.
Math.round(150 * 900 / 10_000);    //  14  on the invoice
Math.round(-150 * 900 / 10_000);   // -13  on the credit note: one cent left over

Rounding a half away from zero, in both directions, removes the difference. Doing it with integers only keeps floating point out of the calculation altogether:

/** VAT in cents for a net amount in cents and a rate in basis points (21% = 2100). */
function vatCents(netCents, rateBp) {
  const scaled = Math.abs(netCents) * rateBp;              // an exact integer
  const cents = Math.floor((scaled + 5_000) / 10_000);     // a half rounds up in size
  return Math.sign(netCents) * cents;
}

vatCents(150, 900);    //  14
vatCents(-150, 900);   // -14
Hands sorting coins into a blue compartment tray on a coral desk.
Illustration made for this article, not a screenshot of the software.

Where you round changes the total

Rounding is not only a question of how, but of when. Take ten lines of €1.07 at 21%:

MethodCalculationVAT
Per line10 × round(22.47 cents)€2.20
Once, on the totalround(224.7 cents)€2.25

Five cents of difference on an invoice of €10.70. Both are defensible ways to program it, and they do not agree. Finance OS calculates VAT once per VAT code on the invoice, not per line, so the length of an invoice can never add up rounding differences. Whatever you choose, choose it once and use the same function for the screen, the PDF and the export. CalcAI goes a step coarser and calculates quotes in whole euros for the same reason: one rule, so the three places cannot disagree.

A rate is not a meaning

It is tempting to store the VAT of a line as a percentage. The trouble starts at zero. A zero-rated sale, an exempt sale and a sale where the VAT is reverse-charged to the customer all carry €0 of VAT. They are three different situations, they need different wording on the invoice, and they end up in different places in a VAT return.

So a line needs a code that says which situation it is, next to the rate that applied at the time. Storing both has a second benefit. When a rate changes, old invoices keep the rate they were issued with instead of quietly adopting the new one.

Numbers that never repeat, invoices that never change

The Dutch invoice requirements ask for consecutive invoice numbers, in one or more series, where each number occurs only once. Two design rules follow from that.

First, a number is only handed out after the invoice has passed validation. If you take the number first and validation then fails, you have burned a number and left a gap to explain.

Second, a finalised invoice is a snapshot. The customer's name and address are copied into it at that moment. If the customer record is edited next year, the invoice from this year still says what it said when it was sent. A mistake is corrected with a credit note and a new invoice, not by editing the old one.

What this does not cover

This is how one tool handles amounts, not tax advice. It leaves out multi-currency invoices, where the minor unit and the exchange rate become part of the problem, and cash accounting, which Finance OS does not implement. If your numbers go to a tax return, have the person who files it look at how they are produced.

Sources