Blog · 25 November 2026 · 7 min read

Rounding errors,and where the missing pennies in a calculation come from

A one-penny gap is never random. It comes from the number type, the rounding mode or the moment you round, and each one can be fixed in writing.

A stack of white wireframe coins on a black background, the top coin missing a small sliver outlined in green
Fig. 01A stack of white wireframe coins on a black background, the top coin missing a small sliver outlined in green

Your software calculates money: payroll, an invoice, a quote, VAT, a calculator on your website. Then one day someone finds a penny's difference with the accountant's spreadsheet. Rounding errors look like a detail. They are often the only errors nobody spots, because the result still looks plausible.

I write about this because I found some in my own product. At Moonworkers, the payroll API I founded for small UK businesses, four one-penny errors came out of a systematic check, and every one of them came from a rounding rule. Here is where these gaps come from, and how I now write down the rounding rule of a calculation before it gets coded.

01Why 0.1 + 0.2 is not 0.3 in software

The first source of error has nothing to do with the rounding you chose. It comes from how the computer stores numbers. The Python documentation puts it simply: floating-point numbers are stored as binary fractions, and most decimal fractions, 0.1 included, have no exact binary form. They are only approximated.

Tom Scott shows it in nine minutes in a Computerphile video. Type 0.1 + 0.2 into a programming language and you get something like 0.30000000000000004. He compares floating point to scientific notation in base 2 that runs out of digits, like writing a third as 0.333 and stopping somewhere. In a video game you never notice. With money, you notice straight away.

It gets sneaky when you round. Python's round() function gives 2.67 for round(2.675, 2), not 2.68. That is not a bug: the stored number is actually a little under 2.675, and the function rounds what it was given.

The fix is well known. The Computerphile video gives two options: a decimal type, or integers in pence or cents that you divide by 100 at the end. The decimal module documentation adds a telling example: in decimal, 0.1 + 0.1 + 0.1 − 0.3 is exactly zero, whereas in floating point the result is tiny but not zero, which breaks equality tests and accumulates.

02Rounding is not a single rule

Once you have the right number type, you still have to decide how to round. People often assume there is only one way. There are several, and they give different results on the cases that fall exactly halfway.

These are the most common:

  • to the nearest, half up: 2.5 gives 3 and 0.125 gives 0.13 to the penny;
  • to the nearest, half to even, often called banker's rounding: 2.5 gives 2, 3.5 gives 4 and 0.125 gives 0.12;
  • truncation, which cuts without looking further: 0.129 gives 0.12;
  • always down, or always up, often imposed by a rule.

Python's decimal module names all of them and uses half to even by default. The round() function does the same. A spreadsheet, a database or a JavaScript library may follow a different convention. If your website works out a quote in JavaScript and your ERP calculates it again on its side, you may have two different rules without knowing it.

Negative numbers add a trap. A credit note of −0.125 gives −0.13 if you round "away from zero", and −0.12 if the rule pushes halves towards plus infinity. So always test a refund, not just positive amounts.

03When you round changes the total

This is the hardest cause of error to spot. Take an invoice with ten lines at £0.43 each before VAT, each carrying £0.086 of VAT. Rounded line by line, each VAT figure becomes £0.09, so £0.90 in total. Calculated on the £4.30 net total, the VAT is £0.86. Four pence apart, with no mistake in the arithmetic.

Both methods can be correct. What isn't is mixing them, or not knowing which one you apply. HMRC's VAT guide is very clear on this. Its section on rounding VAT on invoices allows a line-by-line calculation, rounded down to the nearest 0.1p or to the nearest penny, and then asks you to be consistent. It also says retailers must not simply round VAT down.

Keep this in mind: where you round is part of the rule. A specification that says "round to the penny" without saying where is incomplete.

04What four one-penny errors taught me

At Moonworkers, which is in my project portfolio, we wrote an "oracle": a separate implementation taken straight from HMRC's texts. It replayed 896 National Insurance cases. It found four one-penny errors, all caused by a rounding rule.

Yet the rule was written down in black and white. HMRC's manual on the exact percentage method asks you to calculate employee and employer contributions separately, and to round each to the nearest penny, disregarding amounts of less than £0.005. One sentence. It gives the precision, the mode and the moment of rounding all at once.

I took two things from it. First, our existing tests saw nothing, because they expected the answer the code already gave. It took an independent source to see the gap, which is the whole point of my piece on test oracles for business calculations. Second, a penny in payroll software repeats for every employee, every month. We applied the same care to the free calculators on the site, each checked one by one against the official tools.

05Writing the rounding rule of a calculation, step by step

This is the method I now follow for any calculation that involves money. It fits on one page of the specification.

1. Find the source of the rule

An official text, a contract, an accounting convention, or failing that a decision made with the business. I note the exact reference. For UK payroll, that is the HMRC manual. For an invoice, it is the VAT rules of the country concerned.

2. Write down the four parameters

For each rounded amount, I write:

  1. the precision: the penny, a tenth of a penny, the pound;
  2. the mode: nearest half up, half to even, down;
  3. the moment: per line, on the total, at every intermediate step;
  4. what gets rounded separately, like HMRC's two contributions.

If one of the four is missing, the rule isn't finished. It is the same lesson as on my integration work: the reference data has to be explicit, or every tool invents its own.

3. Choose how numbers are represented

Integers in pence, or a decimal type. Never floating point for an amount of money. In Python, for example, I build the decimal from the text "2.675", then round it to the penny with the half-up mode, and I get 2.68 as expected. If I build it from the float 2.675, the storage error comes straight back, because the value it receives is already slightly under 2.675.

4. Test the edges

Odd and even halves (2.5 and 3.5), the exact threshold of the rule (0.005), a negative amount, an invoice with many lines, zero. That is where rules diverge. Ordinary cases almost always pass.

5. Version the rule with the specification

A rounding rule rarely changes, but it does change. I keep it in the same document as the rest of the calculation, and I version the specification so everyone is talking about the same version.

06What this method does not solve

It does not settle an ambiguous text. When the official rule leaves a choice, as HMRC's VAT guide does between two ways of rounding per line, someone has to decide, and I would rather that person came from the business or the accounts team than from the developers.

Nor does it fix a system you don't control. If your ERP or accounting software rounds its own way, you can document and test the difference, but rarely remove it. In that case I agree with the team which tool is the reference, and the other one follows it.

Finally, it costs time up front. For a simple internal tool that shows rough figures, I wouldn't do all of this. For payroll, an invoice or a tax calculation, it is the minimum, and it is what I took from our own errors. If your calculation feeds into filings, my piece on payroll API compliance goes further into what the tax authority expects.

07Where to start with your own calculation

Take the money calculation that would hurt most if it were wrong. Look in the code or the configuration for where it rounds, and with which function. Check that amounts never go through a floating-point number.

Then write the rule with its four parameters, and test ten cases, five of them at the edges. If you find a one-penny gap, at least you know where it comes from. This is the kind of check I carry out on short engagements, with the team that maintains the calculation, if you would like to talk it through.

Frequently asked questions

What is the rule for rounding?
There isn't just one. At school you round to the nearest value and a 5 goes up. In software and finance you also meet round half to even (often called banker's rounding), truncation and always rounding down. The right rule is the one set by whatever governs your calculation. If nothing does, I agree it with the business and write it into the specification.
What is 3.5 rounded to the nearest whole number?
It gives 4, whether you round half up or half to even, because 4 is even. The difference shows up on 2.5, which gives 3 in the first case and 2 in the second. That is why I always test several halves, odd and even, when I check a rounding rule.
How do you round to two decimal places?
Keep two digits after the decimal point and look at the third. Rounding to the nearest, 12.344 gives 12.34 and 12.346 gives 12.35. The awkward case is 12.345, exactly halfway, where everything depends on the mode you chose. In software, I do this rounding on a decimal type, never on a floating-point number.
How do you round in Excel?
Use ROUND(value, 2) for two decimal places, or ROUNDDOWN and ROUNDUP to force the direction. The number format only changes what you see. The cell keeps all its decimals and adds them up, which is why totals can look a penny out. If the displayed figure matters, round with a formula.

Sources

  1. 01Python Software Foundation, Floating-point arithmetic: issues and limitations
  2. 02Computerphile (Tom Scott), Floating Point Numbers, YouTube video
  3. 03Python Software Foundation, Built-in functions: round()
  4. 04Python Software Foundation, decimal: decimal fixed-point and floating-point arithmetic
  5. 05HM Revenue & Customs, VAT guide (VAT Notice 700), section 17.5: rounding of amounts
  6. 06HM Revenue & Customs, National Insurance Manual NIM11002: exact percentage method