Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
Comment | File | Size | Author |
---|---|---|---|
#8 | advpoll-percentage_total_add_up_100-1799676-8.patch | 23.62 KB | tripper54 |
Comments
Comment #1
ITMonkey CreditAttribution: ITMonkey commentedround() 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.
Comment #2
ITMonkey CreditAttribution: ITMonkey commentedCurrently 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?
Comment #3
nlisgo CreditAttribution: nlisgo commentedHere 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%
Comment #4
nlisgo CreditAttribution: nlisgo commentedAddressed indentation:
Comment #5
soulston CreditAttribution: soulston commentedSince you are working to whatever number of decimals in $precision, we should show this in the output.
Comment #6
nlisgo CreditAttribution: nlisgo commentedI completely agree with the addition to the patch in #5.
Comment #6.0
nlisgo CreditAttribution: nlisgo commentedupdated with issue summary template
Comment #7
tripper54 CreditAttribution: tripper54 commentedRegardless 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.
Comment #8
tripper54 CreditAttribution: tripper54 commentedNew 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.
Comment #9
tripper54 CreditAttribution: tripper54 commentedCommitted.