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

rounding, ruby

Solution

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.

Problem

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?

Original source

Related problems