Created
September 28, 2015 14:05
-
-
Save astronom/9806c7d517f340e2abc1 to your computer and use it in GitHub Desktop.
How to make rounded percentages add up to 100%
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from http://stackoverflow.com/questions/13483430/how-to-make-rounded-percentages-add-up-to-100 | |
function foo(l, target) { | |
var off = target - _.reduce(l, function(acc, x) { return acc + Math.round(x) }, 0); | |
return _.chain(l). | |
sortBy(function(x) { return Math.round(x) - x }). | |
map(function(x, i) { return Math.round(x) + (off > i) - (i >= (l.length + off)) }). | |
value(); | |
} | |
foo([13.626332, 47.989636, 9.596008, 28.788024], 100) // => [48, 29, 14, 9] | |
foo([16.666, 16.666, 16.666, 16.666, 16.666, 16.666], 100) // => [17, 17, 17, 17, 16, 16] | |
foo([33.333, 33.333, 33.333], 100) // => [34, 33, 33] | |
foo([33.3, 33.3, 33.3, 0.1], 100) // => [34, 33, 33, 0] | |
function func( orig, target ) { | |
var i = orig.length, j = 0, total = 0, change, newVals = [], next, factor1, factor2, len = orig.length, marginOfErrors = []; | |
// map original values to new array | |
while( i-- ) { | |
total += newVals[i] = Math.round( orig[i] ); | |
} | |
change = total < target ? 1 : -1; | |
while( total !== target ) { | |
// select number that will be less affected by change determined | |
// in terms of itself e.g. Incrementing 10 by 1 would mean | |
// an error of 10% in relation to itself. | |
for( i = 0; i < len; i++ ) { | |
next = i === len - 1 ? 0 : i + 1; | |
factor2 = errorFactor( orig[next], newVals[next] + change ); | |
factor1 = errorFactor( orig[i], newVals[i] + change ); | |
if( factor1 > factor2 ) { | |
j = next; | |
} | |
} | |
newVals[j] += change; | |
total += change; | |
} | |
for( i = 0; i < len; i++ ) { marginOfErrors[i] = newVals[i] && Math.abs( orig[i] - newVals[i] ) / orig[i]; } | |
for( i = 0; i < len; i++ ) { | |
for( j = 0; j < len; j++ ) { | |
if( j === i ) continue; | |
var roundUpFactor = errorFactor( orig[i], newVals[i] + 1) + errorFactor( orig[j], newVals[j] - 1 ); | |
var roundDownFactor = errorFactor( orig[i], newVals[i] - 1) + errorFactor( orig[j], newVals[j] + 1 ); | |
var sumMargin = marginOfErrors[i] + marginOfErrors[j]; | |
if( roundUpFactor < sumMargin) { | |
newVals[i] = newVals[i] + 1; | |
newVals[j] = newVals[j] - 1; | |
marginOfErrors[i] = newVals[i] && Math.abs( orig[i] - newVals[i] ) / orig[i]; | |
marginOfErrors[j] = newVals[j] && Math.abs( orig[j] - newVals[j] ) / orig[j]; | |
} | |
if( roundDownFactor < sumMargin ) { | |
newVals[i] = newVals[i] - 1; | |
newVals[j] = newVals[j] + 1; | |
marginOfErrors[i] = newVals[i] && Math.abs( orig[i] - newVals[i] ) / orig[i]; | |
marginOfErrors[j] = newVals[j] && Math.abs( orig[j] - newVals[j] ) / orig[j]; | |
} | |
} | |
} | |
function errorFactor( oldNum, newNum ) { | |
return Math.abs( oldNum - newNum ) / oldNum; | |
} | |
console.log( newVals ); | |
return newVals; | |
} | |
func([16.666, 16.666, 16.666, 16.666, 16.666, 16.666], 100); // => [16, 16, 17, 17, 17, 17] | |
func([33.333, 33.333, 33.333], 100); // => [34, 33, 33] | |
func([33.3, 33.3, 33.3, 0.1], 100); // [34, 33, 33, 0] | |
func([13.25, 47.25, 11.25, 28.25], 100 ); // [13, 48, 11, 28] | |
func([13.626332, 47.989636, 9.596008, 28.788024], 100); // => [48, 29, 13, 10] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment