From fc92166d5ffca6303d6fb6170923f7bb462267cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Thu, 14 Jan 2016 01:59:46 +0100 Subject: [PATCH] FIX: when rouding numbers in poll resuls, *don't* select a never-been-voted-for option to round up from zero --- .../poll/assets/javascripts/lib/even-round.js.es6 | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/plugins/poll/assets/javascripts/lib/even-round.js.es6 b/plugins/poll/assets/javascripts/lib/even-round.js.es6 index 3dcc57861c6..0395f1f16a6 100644 --- a/plugins/poll/assets/javascripts/lib/even-round.js.es6 +++ b/plugins/poll/assets/javascripts/lib/even-round.js.es6 @@ -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)); };