mirror of
https://github.com/discourse/discourse.git
synced 2025-09-07 12:02:53 +08:00
FIX: properly round % so they add up to 100 in polls
This commit is contained in:
parent
3a1979d03c
commit
32b6ccd622
2 changed files with 21 additions and 5 deletions
8
plugins/poll/assets/javascripts/lib/even-round.js.es6
Normal file
8
plugins/poll/assets/javascripts/lib/even-round.js.es6
Normal file
|
@ -0,0 +1,8 @@
|
|||
// stolen from http://stackoverflow.com/a/13485888/11983
|
||||
export default (percentages) => {
|
||||
const off = 100 - _.reduce(percentages, (acc, x) => acc + Math.round(x), 0);
|
||||
return _.chain(percentages)
|
||||
.sortBy(x => Math.round(x) - x)
|
||||
.map((x, i) => Math.round(x) + (off > i) - (i >= (percentages.length + off)))
|
||||
.value();
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue