"be consistent" PerlMonks

Re^2: accurately rounding numbers for percentages

by derekn (Initiate)
 on Aug 02, 2009 at 22:53 UTC ( #785296=note: print w/replies, xml ) Need Help??

So i'm gonna have to live with "37%, 23%, 9%, 16%, 16%" (rounded values) equalling 101, even though it SHOULD equal 100%?
• Comment on Re^2: accurately rounding numbers for percentages

Replies are listed 'Best First'.
Re^3: accurately rounding numbers for percentages
by ww (Archbishop) on Aug 02, 2009 at 23:45 UTC
That's not what Trimbach said, by a long shot.

If you add the UNrounded numbers percentages, they should total 100% (except for the fact that you'll sometimes run into value/count pairs that are rounded at the end of whatever length decimal value you use: 100/6, for example).

But, for cases such as I infer yours is, a quite standard and commonly accepted practice is to include the disclaimer "Totals may not equal 100% because of rounding."

Update: For clarity (in light of OP's next reply), s/numbers/percentages/ at strikeout above.

Hmm, I don't know that I made myself clear, so I will go into more detail to make sure we are on the same page. The users vote for which search engine they used to find site. (Google, Yahoo, Bing) The votes are stored in database as total votes for EACH search engine (Google=30,Yahoo=23,Bing=13) Then, I am figuring out the percentage of each search engine used by dividing by total number of votes (\$searchEngine/\$totalVotes*100) which of course does not always yield nice results. This number (\$google,\$yahoo,\$bing) is then rounded. The problem of course is that sometimes the 3 different percentages don't add up to 100%. Derek
Your made yourself clear enough in the OP but you don't seem to like the answers (or won't understand them?).

The numbers in your latest post, 30, 23, and 13, happen to be what I gather your mean by a "nice" case; that is, when calculated as percentages of their total and rounded, the percentages add up (nicely) to 100.

Here's a case where the numbers are not "nice" and where a disclaimer is the only reasonable solution (fudging is NOT, IMO, reasonable):

```#!/usr/bin/perl
use strict;
use warnings;
#785290

my \$Google=30; my \$Yahoo=23; my \$Bing=12;
my \$sum=\$Google + \$Yahoo + \$Bing;

my \$yahoo_percent = \$Yahoo/\$sum;
print "\tY: \$yahoo_percent";
my \$bing_percent = \$Bing/\$sum;
print "\tB: \$bing_percent \n";

my \$total = (\$google_percent + \$yahoo_percent + \$bing_percent);

my \$roundedY = sprintf("%.2f", \$yahoo_percent);
my \$roundedB = sprintf("%.2f", \$bing_percent);
my \$rounded = sprintf("%.2f", \$total);
print "Google: \$roundedG; Yahoo: \$roundedY; Bing: \$roundedB; Total: \$r
+ounded \n";

print "%\n";
printf ('%.0f', ((\$Yahoo/\$sum)*100));
print "%\n";
printf ('%.0f', ((\$Bing/\$sum)*100));
print "%\n";
print \$rounded*100 . "% (Percentages may not add up to 100 because of
+rounding errors!)\n";
So do what ww said and simply put a disclaimer about the numbers being rounded and not necessarily adding up to 100%.

You can't introduce error to your numbers via rounding and then magically expect that error to disappear and have everything add up to 100 all the time. People are smart enough to understand rounding errors if you add a comment to that effect.

Gary Blackburn
Trained Killer

Create A New User
Node Status?
node history
Node Type: note [id://785296]
help
Chatterbox?
 [Corion]: Discipulus: I think Module::Corelist ( or corelist ) can tell you that [Corion]: Meh - Module::CoreList [choroba]: Just use List::Util 1.29 [choroba]: Be kind to people using old Perl with newer version of the module [Corion]: But why not simply specify 'List::Util' => 1.29 in your prerequisites? People might install a more recent version of the module into their Perl [Corion]: (what choroba said) [Discipulus]: ah! simpler and cleaner

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (12)
As of 2017-05-25 09:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favorite model of computation is ...

Results (187 votes). Check out past polls.