34

Suppose we have a list of items with an integer:

USA:       3 people
Australia: 2 people
Germany:   2 people

If we calculate the percentage of each value against the sum over the whole list, we get:

USA:       3/(3+2+2)*100 = 42.857...%
Australia: 2/(3+2+2)*100 = 28.571...%
Germany:   2/(3+2+2)*100 = 28.571...%

and if we round it, we get:

USA:       43%
Australia: 29%
Germany:   29%

The sum 43+29+29 = 101 is not 100, and it looks a little bit strange to the user of the software. How would you solve this problem?

sawa
  • 165,429
  • 45
  • 277
  • 381
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746
  • 3
    There is no real way around this. The problem can also work with rounding the other way. Eg, 11 countries, all with 2 people each. Using the same math, will only add up to 99%. And If you had 200 countries, all with 1 person, then they would all have 0%. – Adam Harte Mar 08 '11 at 02:08
  • Definitely related: https://stackoverflow.com/questions/13483430/how-to-make-rounded-percentages-add-up-to-100 – John Y Sep 13 '17 at 22:50

5 Answers5

39

You can refer to the Largest Remainder Method used in election: Wikipedia: Largest Remainder Method

In your case, you have

USA:       42.857...
Australia: 28.571...
Germany:   28.571...

If you take the integer part, you get

USA:       42
Australia: 28
Germany:   28

which adds up to 98, and you want to take two more. Now, you look at the decimal parts, which are

USA:       0.857...
Australia: 0.571...
Germany:   0.571...

and take the largest ones until the total reaches 100. If you take USA, the total becomes 99, and you want to take one more. Here the problem arises. Since you are left with a tie of 0.571... between Australia and Germany, if you take both, the total will be 101. So you have two ways to choose from:

(a) If you strongly want the total to be 100, just take Australia, and take no more:

USA:       43
Australia: 29
Germany:   28

(b) If you rather want to respect the fact that Australia and Germany are in a tie, you stop at that point:

USA:       43
Australia: 28
Germany:   28
sawa
  • 165,429
  • 45
  • 277
  • 381
  • 3
    i just implemented a gem which utilizes the largest remainder method, you may check out under https://github.com/jethroo/lare_round and are welcome to improve it further ;) – jethroo Aug 13 '13 at 23:27
  • 1
    Thanks for the link, I've never heard of it, but helped me creation my function =) – Enissay Aug 22 '13 at 22:18
15

Option 1

If you are concerned about the results looking a bit strange to the user, I would put a footnote regarding the results mentioning that percentages have been rounded and may not total to 100%. You could programmatically display the message only when the rounding causes this behavior.

USA percentage:       43
Australia percentage: 29
Germany percentage:   29

*Percentages may not total 100 due to rounding

Option 2

Since you are using Ruby, I would suggest using rational numbers. This way you don't lose the precision when needed. Instead of the footnote, you might display the percentage with the rational numbers next to it like the following:

USA percentage:       43 (3/7)
Australia percentage: 29 (2/7)
Germany percentage:   29 (2/7)

Option 3

Include more decimal places so that the rounding error is less severe:

USA percentage:       42.9
Australia percentage: 28.6
Germany percentage:   28.6

This results in 100.1 instead of 101.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
Matt
  • 14,353
  • 5
  • 53
  • 65
  • Including more decimal places sounds like a good solution/fix. – Adam Harte Mar 08 '11 at 02:04
  • 4
    Adding one decimal place indicates to the viewer that these are not whole numbers and therefore can be expected to have been rounded. Many people, upon seeing a whole number with no decimals, will assume that it is the precise number rather than a rounded number. Thus, including one decimal in the answer is a great idea. – Thorin Jan 24 '13 at 20:44
5

Components may not sum to totals because of rounding. Standard in statistical reports.

The cheats will come back to bite you in the next table, report or appendix. You can't keep track of the changes. It only looks funny to people who don't read these kind of reports.

Oh yeah. Its not a rounding error.

dom
  • 51
  • 1
  • 1
  • 1
    I agree with you that it only looks funny to people who don't read these kind of reports, and this kind of cheat is after all unnecessary. However, this cheat is probably just for displaying, and the internal data will be kept as is, so it will not affect the next table and so on. – sawa Oct 02 '12 at 15:55
4

You may "cheat" a bit by summing all the rounded results but the last and giving to the last one the value of 100 - the previous sum...

In this case, you would have :

USA = 43
Aus = 29
Ger = 28  (100 - (43 + 29))

But that's only a dirty trick... You should rather follow the more honest/accurate solution given by Matt as mine seems to indicate that Germany percentage is less than Australian one.

jaco
  • 1,007
  • 1
  • 6
  • 8
  • 4
    If you're going to employ the dirty trick, apply the correction to the item where doing so will produce the smallest error. – kindall Mar 08 '11 at 01:57
1
    const indexes = [];
const decimalPercentages = [
  0,
  6.501707128047701,
  80.72721472499585,
  10.985525877598509,
  1.7855522693579489
];
const sum = decimalPercentages.map(d => d).reduce((a, b) => a + b, 0);
let percentages = decimalPercentages.map(d => Math.floor(d));
const percentagesTotal = percentages.reduce((a, b) => a + b, 0);

// Use the largest Remainder Method to get percentages total at 100

if (100 - percentagesTotal) {
  const decimalValues = decimalPercentages.map((d) => {
    if (!d.toString().split('.')[1]) {
      return 0;
    }
    return d.toString().split('.')[1];
  });
  const targetIndexes = [...decimalValues].sort().splice(percentages.length -
    (100 - percentagesTotal));
  targetIndexes.forEach((element) => {
    indexes.push(decimalValues.indexOf(element));
  });
  percentages = percentages.map((element, key) => {
    if (indexes.includes(key)) {
      return element + 1;
    }
    return element;
  });
}
Pharis Kahama
  • 111
  • 1
  • 6