From 4f25278b39aae965d874f21d1fd56128630d5b25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 23 Nov 2015 15:28:24 +0100 Subject: [PATCH] FIX: round down percentages when using multiple choices polls --- .../components/poll-results-standard.js.es6 | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/plugins/poll/assets/javascripts/components/poll-results-standard.js.es6 b/plugins/poll/assets/javascripts/components/poll-results-standard.js.es6 index e0ae0071a..17dece580 100644 --- a/plugins/poll/assets/javascripts/components/poll-results-standard.js.es6 +++ b/plugins/poll/assets/javascripts/components/poll-results-standard.js.es6 @@ -5,16 +5,21 @@ export default Em.Component.extend({ tagName: "ul", classNames: ["results"], - @computed("poll.voters", "poll.options.[]", "poll.isMultiple") + @computed("poll.voters", "poll.options.[]", "poll.type") options() { const options = this.get("poll.options"); const voters = this.get("poll.voters"); + let percentages = voters === 0 ? Array(options.length).fill(0) : _.map(options, o => 100 * o.get("votes") / voters); - // fix percentages to add up to 100 when the poll is single choice only - if (!this.get("poll.isMultiple")) { + // properly round percentages + if (this.get("poll.type") === "multiple") { + // when the poll is multiple choices, just "round down" + percentages = percentages.map(p => Math.floor(p)); + } else { + // when the poll is single choice, adds up to 100% percentages = evenRound(percentages); }