|
|Store|
|
|
|
|
|
|
|
CTK Exchange
Charter Member
|
Dec--, 00: AM (EST) |
|
"Dear Alexander,
Problem: To make all integer percentages add up to 100%
To calculate the percentages, I divide each number by the total of all the numbers (and multiply by 100). A this point of course they all add up to 100, however I need integer percentages, so my problem really occurs in the conversion from floating point numbers to integers.
Here is an example of my problem
If the percentages work out to be
10.2
30.65
28.55
30.6
These floting point numbers all add up to 100. The integer parts add up to 98. If I round the values ie( 10,31,29,31) they add up to 101.
One way I thought about doing it was to add up all the fraction parts in this case (0.2+0.65+0.55+0.6) = 2. Therefore the rounding process should generate an additional 2%, I could then order the fractional parts (0.2, 0.55, 0.6, 0.65) Starting at the right hand side I could round up 2 values and round the rest down.
ie
Round the last 2 numbers up
[pre]
.65 rounds to 1 (add 1 to the integer part = 31)
.6 rounds to 1 (add 1 to the integer part = 31)
[/pre]
and now round the rest down
[pre]
.55 rounds to 0 (add 0 to the integer part = 28)
.2 rounds to 0 (add 0 to the integer part = 10)
[/pre]
The percents then all add up to 100, (31+31+28+10) = 100
I was hoping there was a simpler approach...
TIA
Chris Morris
"
Attachments
https://www.cut-the-knot.org/htdocs/dcforum/User_files/ChrisMorrisGuest
|
Alert | IP |
Printer-friendly page | Edit |
Reply |
Reply With Quote | Top |
|
|
You may be curious to visit the old CTK Exchange archive.
|Front page|
|Contents|
Copyright © 1996-2018 Alexander Bogomolny
[an error occurred while processing this directive]
|
Advertise
|