Polls percentage accounting is not accurate

It seems when calculating percentage for each option in polls Discourse uses floats truncating instead of rounding. This can result into inaccurate percentage values. For instance, a poll with 2 options, 11 voters and votes spread as 5 for the 1st option and 6 for the 2nd option indicates 45% and 54% which is 99% in total but should be 100%.

The math looks to be the following:

  • 100/11*5 is 45.45, which rounded to 0 decimal places is 45, and also truncated to 0 decimal places is 45.
  • 100/11*6 is 54.54, which rounded to 0 decimal places would be 55, but truncated to 0 decimal places is 54.

Would be correct to indicate 55% instead of 54%, it would give the correct total value.

2 Likes

Just pushed a fix :poultry_leg:

https://github.com/discourse/discourse/commit/32b6ccd6227e561d5ac8f5443e766344402a946b

6 Likes

This topic was automatically closed after 12 hours. New replies are no longer allowed.