accurately calculating percentages with perl

Reply

Join Date: May 2005
Posts: 45
Reputation: derekn is an unknown quantity at this point 
Solved Threads: 0
derekn derekn is offline Offline
Light Poster

accurately calculating percentages with perl

 
0
  #1
Aug 2nd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: accurately calculating percentages with perl

 
0
  #2
Aug 2nd, 2009
use sprintf to round off your numbers.

  1. my $t = 92.38495;
  2. $t = sprintf ("%.0f", $t);
  3. print $t;
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Perl Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC