Re:0.30000000000000004
by protonfish on Sunday April 02, 2017 @03:36PM
(#14018611)
Using tricks to make numbers total what you think they "should" other than using rounding to proper amount of precision is lazy and deceitful.
Depends on the application of this, i.e. whether the total is more important or the individual values are more important.
Example: You're filling out a timesheet for a contracting job, and you worked 8 hours on several different tasks for your client, but your client's software rounds things to the nearest hour, then it would make sense to use an algorithm like this if your pay was going to be determined by this data entry. If your pay was not determined by this data entry then it may make sense to just round normally.
You should just round the percentages to the nearest value of the appropriate precision, and let them sum to slightly more or less than 100% if that’s how the numbers work out. If you want, add an asterisk and a “note, numbers do not sum to 100% because of rounding” at the bottom.
For those interested, the largest remainder method (https://gist.github.com/hijonathan/e597addcc327c9bd017c) is useful for dealing with this.