Make rounded numbers add up to the total
Rounded parts often miss their rounded total, because each part's rounding error is thrown away on its own. To make them agree, either give the missing units to the parts with the largest remainders (the largest remainder method), or round a running total and show the differences, so the lines telescope to the total. The tool below shows both on a real score receipt, computed in whole units with no floating point.
- Updated
- Reading
- 5 min
- The need
- Show a breakdown whose rounded lines add up exactly to the rounded total, without changing the total.
- Plain text
- /guides/rounded-numbers-add-up.md
| Line | Exact | Each alone | Largest remainder | Running total |
|---|---|---|---|---|
| Work | +12.539872 | +12.54 | +12.54 | +12.54 |
| Momentum | +3.024910 | +3.02 | +3.03 | +3.02 |
| People | +1.562561 | +1.56 | +1.56 | +1.57 |
| People | +0.995928 | +1.00 | +1.00 | +0.99 |
| Other | −0.569189 | −0.57 | −0.57 | −0.57 |
| Catch-up | −2.885174 | −2.89 | −2.89 | −2.88 |
| Sum | +14.668908 | +14.66 ✗ | +14.67 ✓ | +14.67 ✓ |
The total rounds to +14.67. Rounding each line alone adds up to +14.66, so the lines and the total disagree.
#Why rounded parts miss the total
Split 1 into three equal parts and show one decimal: each part is 0.3, and three of them make 0.9, not 1.0. Nothing is wrong with any single line. Each one was rounded correctly; the three small errors just point the same way.
Every rounded line can be off by up to half a unit. With ten lines, the sum can be off by up to five units, although one or two is far more common. Readers notice quickly, and a breakdown that doesn't add up makes every number on the page look doubtful.
#Three ways to round a breakdown
| Method | Lines add up to the rounded total | Each line within one unit of its exact value | Depends on line order |
|---|---|---|---|
| Each line alone | No | Within half a unit | No |
| Largest remainder | Yes | Yes | Only to break ties |
| Running total | Yes | Yes | Yes |
The tool at the top of this page shows all three side by side. Change the number of decimals to see the first column drift while the other two hold.
#Largest remainder, step by step
- Work in whole units of the smallest step you will show. For two decimals, count in hundredths.
- Round the exact total once. This is the number the lines must reach.
- Round every line down, and add up those floors.
- The shortfall is a whole number of units. Give one unit each to the lines with the largest remainders, until the shortfall is gone.
Each line ends at its floor or one unit above it, so no line moves by more than one unit. Rounding down works for negative values too: −0.178861 at two decimals has a floor of −0.18 and a remainder of 0.001139. Decide ties in a fixed order, for example the larger absolute value first and then the original position, so the same input always gives the same output.
#Running total
Add the exact lines one at a time, round each running total, and show the difference between consecutive rounded totals. The last running total is the rounded total, and every difference in between cancels, so the lines add up by construction. A sum that collapses like this is called telescoping.
The catch is order. Move a line up or down the list and the rounded values can change, even though nothing about the line did. Use this method when the order is fixed and meaningful, such as steps in time.
#Make the exact lines add up first
Rounding for display only works if the exact lines already add up. When a breakdown explains a change in a computed score, the reliable way to get there is to compute each line as the difference between two successive evaluations: the score before anything moved, then after the first reason, then after the second, and so on. The lines telescope to the full change, whatever the model does in between. Store them as integers in a fine unit, such as millionths, so the sum is exact rather than approximately exact.
#Common mistakes
- Rounding the total from the rounded lines. The total then agrees with the lines but disagrees with the same number shown elsewhere. Round the exact total, and make the lines reach it.
- Doing the arithmetic in floating point. 0.1 + 0.2 is not 0.3 in binary floating point. Count in integers, and convert to decimals only for display.
- Always adding the display gap to the biggest line. It works, but at display precision one line can absorb several units and visibly differ from its own exact value.
- Adding a "rounding" line. It makes the sum work and gives the reader a line that explains nothing.
- Rounding a small line to zero and dropping it. Keep zero lines out of the input instead, so nothing quietly disappears after rounding.
#Questions
Which method should I use?
Largest remainder for categories without a natural order, such as the parts of a score or the slices of a chart. A running total for a sequence, such as a balance over time.
Can a line change sign after rounding?
No. A line smaller than one unit can round to zero, never to the opposite sign. At two decimals, −0.004 may show as 0.00 or as −0.01. If the direction of a small line matters, show more decimals.
Why not show more decimals instead?
More decimals shrink the gap but don't remove it, and they make the breakdown harder to read. Two decimals that add up are better than six that almost do.
Does this apply to percentages?
Yes. Treat 100% as the total and the percentages as the lines. The largest remainder method is the usual way to make a table of percentages add up to exactly 100.
#How Upvera does it
Upvera Value comes with a receipt for every change. Each receipt line is the difference between two successive evaluations of the same rule, so the lines telescope to the change itself. The server stores the lines in whole millionths. When a line has to absorb a rounding remainder at that scale, it is the line with the largest effect; at that scale the gap is a few millionths, so no line visibly moves. Clients don't compute amounts; they draw the server's integers.
The Upvera apps show receipt lines at two decimals using the largest remainder method, in integer arithmetic, so the lines on screen add up to the change on screen. The tool above uses the same method on Upvera's own public receipt. Upvera Value is computed by Upvera and unitless; it is not a price and not what a company is worth. More in the Value Engine report.
#Sources
- Largest remainder method: the method, also known as Hamilton's method.
- Apportionment paradox: the known edge cases of this kind of rounding.
- Telescoping series: why consecutive differences sum to the end minus the start.
- Upvera's public receipt: the data in the tool.
- How Upvera Value works: what the receipt explains.