2016-11-03 17:11:17 +08:00
|
|
|
// works as described on http://stackoverflow.com/a/13483710
|
2015-11-30 18:24:25 +08:00
|
|
|
function sumsUpTo100(percentages) {
|
|
|
|
return percentages.map((p) => Math.floor(p)).reduce((a, b) => a + b) === 100;
|
|
|
|
}
|
|
|
|
|
2016-12-08 04:48:47 +08:00
|
|
|
export default function (percentages) {
|
2021-01-27 19:39:20 +08:00
|
|
|
let decimals = percentages.map((a) => a % 1);
|
2016-11-03 17:11:17 +08:00
|
|
|
const sumOfDecimals = Math.ceil(decimals.reduce((a, b) => a + b));
|
|
|
|
// compensate error by adding 1 to n items with the greatest decimal part
|
|
|
|
for (let i = 0, max = decimals.length; i < sumOfDecimals && i < max; i++) {
|
|
|
|
// find the greatest item in the decimals array, set it to 0,
|
|
|
|
// and increase the corresponding item in the percentages array by 1
|
|
|
|
let greatest = 0;
|
|
|
|
let index = 0;
|
2018-06-16 00:42:20 +08:00
|
|
|
for (let j = 0; j < decimals.length; j++) {
|
2016-11-03 17:11:17 +08:00
|
|
|
if (decimals[j] > greatest) {
|
|
|
|
index = j;
|
|
|
|
greatest = decimals[j];
|
|
|
|
}
|
2016-01-14 08:59:46 +08:00
|
|
|
}
|
2016-11-03 17:11:17 +08:00
|
|
|
++percentages[index];
|
|
|
|
decimals[index] = 0;
|
|
|
|
// quit early when there is a rounding issue
|
2020-09-22 22:28:28 +08:00
|
|
|
if (sumsUpTo100(percentages)) {
|
|
|
|
break;
|
|
|
|
}
|
2015-11-25 07:14:24 +08:00
|
|
|
}
|
2016-11-03 17:11:17 +08:00
|
|
|
|
2015-11-30 18:24:25 +08:00
|
|
|
return percentages.map((p) => Math.floor(p));
|
2018-06-16 00:42:20 +08:00
|
|
|
}
|