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?
You may notice that sometimes a set of percentages don't always add up to exactly 100%. This is an expected result of rounding to the nearest whole number. For example, three equal responses would give percentages of 33.3% each. When rounded to whole numbers we get 33%, 33% and 33% that together make only 99%.
Select all the cells in the table of rounded numbers (i.e., the right-most table in the example below), and check that they add up to 100%. Manually change one of the numbers in the table so that it does add up to 100%. Excel shows you this in the bottom-right corner of the screen.
Percent Doesn't Add to 100%The percentage of responses may not always add up to 100%. With rounding you can adjust/control the position of the decimal point when customizing your charts or response data.
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
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
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)
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;
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With