Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: accurately rounding numbers for percentages

by Trimbach (Curate)
on Aug 02, 2009 at 22:24 UTC ( #785293=note: print w/replies, xml ) Need Help??

in reply to accurately rounding numbers for percentages

What you're asking is not possible. Anytime you round a number you're going to introduce error, how much error will depend on how much you're rounding. Add enough errors together and your total will always be off from the "expected" total (in this case 100%).

The only way around this is to go ahead and round the individual entries to whole numbers for display, but when calculating the total don't add the rounded entries, add the unrounded entries, and then round the result for display, if you want.

Gary Blackburn
Trained Killer

  • Comment on Re: accurately rounding numbers for percentages

Replies are listed 'Best First'.
Re^2: accurately rounding numbers for percentages
by derekn (Initiate) on Aug 02, 2009 at 22:53 UTC
    So i'm gonna have to live with "37%, 23%, 9%, 16%, 16%" (rounded values) equalling 101, even though it SHOULD equal 100%?
      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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://785293]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2021-09-28 04:59 GMT
Find Nodes?
    Voting Booth?

    No recent polls found