943,654 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 1596
  • Perl RSS
Aug 2nd, 2009
0

accurately calculating percentages with perl

Expand Post »
I am trying to accurately calculate percentages with perl. The problem is of course that percentages don't end up as nice, even whole numbers...and I want to truncate those number (92.38495%) to the whole number accurately reflecting the percentage (92%). Using "($value/$total)*100" gives the percentages, then I used "$value=int($value+0.5)" to round it up or down. The problem is that all the percentages should equal 100 (duh), but sometimes, after being rounded, they DON'T! Sometimes it's 101, sometimes 99, etc. How can I round these numbers off so that they are accurate in equaling 100?

Derek
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
derekn is offline Offline
45 posts
since May 2005
Aug 2nd, 2009
0

Re: accurately calculating percentages with perl

use sprintf to round off your numbers.

perl Syntax (Toggle Plain Text)
  1. my $t = 92.38495;
  2. $t = sprintf ("%.0f", $t);
  3. print $t;
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: script cancels with message but no illegal operation
Next Thread in Perl Forum Timeline: raw data: help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC