Consider the four percentages below, represented as float
numbers:
13.626332% 47.989636% 9.596008% 28.788024% ----------- 100.000000%
I need to represent these percentages as whole numbers. If I simply use Math.round()
, I end up with a total of 101%.
14 + 48 + 10 + 29 = 101
If I use parseInt()
, I end up with a total of 97%.
13 + 47 + 9 + 28 = 97
What's a good algorithm to represent any number of percentages as whole numbers while still maintaining a total of 100%?
Edit: After reading some of the comments and answers, there are clearly many ways to go about solving this.
In my mind, to remain true to the numbers, the "right" result is the one that minimizes the overall error, defined by how much error rounding would introduce relative to the actual value:
value rounded error decision ---------------------------------------------------- 13.626332 14 2.7% round up (14) 47.989636 48 0.0% round up (48) 9.596008 10 4.0% don't round up (9) 28.788024 29 2.7% round up (29)
In case of a tie (3.33, 3.33, 3.33) an arbitrary decision can be made (e.g. 3, 4, 3).
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.
Due to rounding, percentages may not always appear to add up to 100%. You can control how many decimal points to display when you customize a chart or data table.
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%.
There are many ways to do just this, provided you are not concerned about reliance on the original decimal data.
The first and perhaps most popular method would be the Largest Remainder Method
Which is basically:
In your case, it would go like this:
13.626332% 47.989636% 9.596008% 28.788024%
If you take the integer parts, you get
13 47 9 28
which adds up to 97, and you want to add three more. Now, you look at the decimal parts, which are
.626332% .989636% .596008% .788024%
and take the largest ones until the total reaches 100. So you would get:
14 48 9 29
Alternatively, you can simply choose to show one decimal place instead of integer values. So the numbers would be 48.3 and 23.9 etc. This would drop the variance from 100 by a lot.
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