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

use sprintf to round off your numbers.

my $t = 92.38495;
$t = sprintf ("%.0f", $t);
print $t;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.