mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 09:36:19 -05:00
FIX: when rouding numbers in poll resuls, *don't* select a never-been-voted-for option to round up from zero
This commit is contained in:
parent
1a27d887fa
commit
fc92166d5f
1 changed files with 7 additions and 5 deletions
|
@ -5,11 +5,13 @@ function sumsUpTo100(percentages) {
|
|||
|
||||
export default (percentages) => {
|
||||
const sumOfDecimals = Math.ceil(percentages.map(a => a % 1).reduce((a, b) => a + b));
|
||||
// compensate error by adding 1 to the first n items
|
||||
for (let i = 0; i < sumOfDecimals; i++) {
|
||||
percentages[i] = ++percentages[i];
|
||||
// quit early when there is a rounding issue
|
||||
if (sumsUpTo100(percentages)) break;
|
||||
// compensate error by adding 1 to the first n "non-zero" items
|
||||
for (let i = 0, max = percentages.length; i < sumOfDecimals && i < max; i++) {
|
||||
if (percentages[i] > 0) {
|
||||
percentages[i] = ++percentages[i];
|
||||
// quit early when there is a rounding issue
|
||||
if (sumsUpTo100(percentages)) break;
|
||||
}
|
||||
}
|
||||
return percentages.map(p => Math.floor(p));
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue