| | |
accurately calculating percentages with perl
![]() |
•
•
Join Date: May 2005
Posts: 45
Reputation:
Solved Threads: 0
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
Derek
use sprintf to round off your numbers.
perl Syntax (Toggle Plain Text)
my $t = 92.38495; $t = sprintf ("%.0f", $t); print $t;
![]() |
Similar Threads
- even and odd columsn (Shell Scripting)
- Perl threads (Perl)
- Is Perl Dead? (Perl)
- Online Casino Development ?'s (IT Professionals' Lounge)
- perl developer- job vacancy (Web Development Job Offers)
- Perl Developer with Messaging (Software Development Job Offers)
- Looking for perl programmer (Web Development Job Offers)
- need help to solve this problem (Java)
- Calculate the amount of time in the linux process using perl script (Perl)
- OptiPerl - Perl IDE (Perl)
Other Threads in the Perl Forum
- Previous Thread: script cancels with message but no illegal operation
- Next Thread: raw data: help
| Thread Tools | Search this Thread |





