The Penny That Goes Missing: A VAT-Safe Price Loader for UK and EU Baskets
Photo: UK VAT receipt calculator laptop online shopping basket, via upload.wikimedia.org
Picture the scene. A shopper in Leeds sees £19.99 on a product page. They switch delivery to Dublin, and the basket suddenly says €20.49. Nothing is broken, but someone in the room says 'shouldn't it be €19.99?' That is the moment a price loader stops being a formatting detail and becomes a finance problem.
This is a practical pattern for storing net prices once and rendering VAT-inclusive amounts for UK and EU shoppers without turning the browser into a tax engine. It covers where the rules live, how to hydrate without a flicker, and the penny-rounding cases that make people distrust the whole basket.
Keep the net price, ship the gross
Store the net price in minor units — pence, cents — as an integer. Do not store £16.66 as a float and hope. Alongside it, keep the currency and the VAT treatment that applies to the sale. The server calculates the gross amount and the formatted string. The browser gets both, but treats the gross as display data, not a suggestion.
A workable shape is:
type Price = {
netMinor: number;
currency: 'GBP' | 'EUR';
vatCountry: 'GB' | 'IE' | 'DE' | string;
};
The loader can carry the net amount in a data attribute for debugging, but the visible price should come from the server. If you must calculate client-side for a preview, use the same integer maths and label it as an estimate. Never let the client's sum become the number on the invoice.
Where the rules live
Put VAT rates and rounding rules in a server-side configuration table, not in a chain of if (country === 'GB') branches scattered through components. A small rule set looks like this:
GB:
vatBps: 2000
currency: GBP
rounding: half-up
IE:
vatBps: 2300
currency: EUR
rounding: half-up
DE:
vatBps: 1900
currency: EUR
rounding: half-up
That is not a complete EU tax system. B2C distance selling rules, thresholds, zero-rated goods and OSS reporting are real things. The point is architectural: the pricing service owns those rules, and the loader asks for a result. Do not ship the whole EU VAT table to every browser just in case someone changes their delivery country. Load the rule for the country you know about, and fetch a fresh quote when the destination changes.
For UK shoppers, the common case is 20% VAT and GBP. For EU destinations, the rate usually follows the destination country for B2C sales, and the currency may differ. Your loader should accept a country and currency, ask the server, and render what comes back.
Hydration without the flicker
Server-render the final gross price. Then hydrate with the same string. A common failure looks like this: the server does Math.round(net * 1.2) and the client does net * 1.2 and formats it, and for one frame the price twitches from £19.99 to £20.00. Users notice. So do screenshot tests.
A simple markup pattern:
<span class='price'
data-price-net-minor='1666'
data-vat-country='GB'
data-price-gross-minor='1999'>£19.99</span>
The loader can read data-price-net-minor to show a breakdown in a tooltip, but the text node is already correct. If the shopper changes country, call the pricing endpoint and replace the text. Do not recalculate in the browser unless you have a very good reason and a test suite that matches the server.
The penny tests
Rounding arguments are usually about a penny, which is exactly the amount that makes people open a support ticket. Test these cases on the server and in the hydration path:
| Net (minor) | VAT | Gross (minor) | Display |
|---|---|---|---|
| 1666 | 20% | 1999 | £19.99 |
| 1 | 20% | 1 | £0.01 |
| 4 | 20% | 5 | £0.05 |
| 5 | 20% | 6 | £0.06 |
| 833 | 20% | 1000 | £10.00 |
| 1666 | 23% | 2049 | €20.49 |
Use integer arithmetic with an explicit rounding mode:
type VatRule = { vatBps: number; currency: string; rounding: 'half-up' };
export function grossMinor(netMinor: number, rule: VatRule): number {
if (!Number.isInteger(netMinor) || netMinor < 0) {
throw new Error('netMinor must be a non-negative integer');
}
const numerator = netMinor * (10_000 + rule.vatBps);
return Math.floor((numerator + 5_000) / 10_000);
}
That is half-up for positive amounts. If your finance team uses a different convention, encode it once and test it. Do not let two teams round differently and then blame the loader.
The loader bit
The loader's job is small: know the shopper's country, request the right prices, and swap the text. It should not know that Ireland is 23% and Germany is 19%. It should not contain a hard-coded list of EU currencies. It should cache the server response for the basket session and invalidate when the country or currency changes.
A request might look like:
const res = await fetch(`/api/prices?ids=${ids.join(',')}&country=${country}`);
const prices = await res.json();
The response includes grossMinor and formatted. The loader writes formatted into the DOM and keeps grossMinor for analytics. If the request fails, keep the server-rendered price and show a quiet retry. Do not fall back to a client-side VAT guess.
The server is the source of truth
A price loader is not a pricing engine. The server should remain the authority for invoices, refunds, receipts, tax reporting, and any number a customer can screenshot. The browser is allowed to display, cache and compare. It is not allowed to decide.
That division also makes testing sane. You can test the server's rounding table without a browser, and you can test the loader's hydration without a tax law degree. The shopper gets one number, consistently, whether they are in Leeds, Dublin or Berlin. And nobody has to explain a missing penny in the Monday meeting.