开发者

How to deal with the sum of rounded percentage not being 100?

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.开发者_StackOverflow中文版..%
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?


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


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.


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.


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.


    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;
  });
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜