Problem/Motivation

The total percentage does not always add up to 100. Changing the rounding method from floor() to round will fix this in a lot of cases, but not all.

Proposed resolution

round() is the method that suits most use cases. However some users may prefer floor(), so the method should be configurable. There are also cases where displaying percentages more precisely is preferable, so there should be an option to do this.

Remaining tasks

Reviews.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ITMonkey’s picture

Priority: Major » Minor

round() wouldn't work either and you don't want the percentages to be forced to equal 100% as it can skew the true result. If a vote has three possible answers and each receives the same number of votes, the candidates will all have 33.3(r)% and to force one of them to show 33.4 would indicate a winner when in fact there is none.

Marked priority as minor as the choice of which to use, floor() or round(), is actually personal preference. It may make more sense to include some documentation as to how and where to change the code rather than alter the existing code.

ITMonkey’s picture

Currently the code is in advpoll\advpoll_ranking\advpoll_ranking.module at line 621
$percentage = round($rows[0]['votes'] / count($voters) * 100, 1);
and advpoll\includes\advpoll_voteapi.inc at line 165
$percentage = floor((int) $value / (int) $total * 100);

Regardless of which is preferred, should they not be consistent?

nlisgo’s picture

Status: Active » Needs review
FileSize
1.14 KB

Here is a proposed batch which allows you to set a value in the variable table to amend the precision. There is no central admin screen for this module but at least this presents an opportunity to set the precision.

So if you have 9 votes and 3 choices (A - 2 votes, B - 3 votes and C - 4 votes).

Currently you would see:

A - 22%
B - 33%
C - 44%

Total 99%

If you set the variable 'advpoll_percentage_precision' to 1 then you would see:

A - 22.2%
B - 33.3%
C - 44.4%

nlisgo’s picture

Addressed indentation:

soulston’s picture

Since you are working to whatever number of decimals in $precision, we should show this in the output.

nlisgo’s picture

I completely agree with the addition to the patch in #5.

nlisgo’s picture

Issue summary: View changes

updated with issue summary template

tripper54’s picture

Issue summary: View changes

Regardless of the proposed precision patch, I think floor() should be changed to round() in advpoll_calculate_percentage().

I know it won't fix a 3 option, 1/3 each scenario, but it would make a two option vote add up to 100, which I would have thought is a very common use case.

tripper54’s picture

New patch:

* Lets the site admin decide which rounding method to use - 'round' or 'floor'. Defaults to 'round'. This method is used in the two places mentioned in comment #2.
* Adds a configuration form for the module where you can set precision and rounding method.

Sorry there's a bit of noise in the patch. My IDE must've done some auto converting of line breaks to unix format.

tripper54’s picture

Status: Needs review » Fixed

Committed.

  • tripper54 committed 387c556 on 7.x-3.x
    Issue #1799676 by nlisgo, tripper54, soulston: Percentage Total doesn't...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.