OK, I needed a routine to round numbers that have decimal places, and
Perl does not appear to have a *native* function for doing that, so I
poked around on the internet and found this subroutine:
sub roundit
{
my($num, $places) = @_;
return sprintf "%0.*f", $places, $num;
}
I figured that since it uses sprintf, it would be fine, but I decided to
test it anyway and I'm getting some odd results. The exact program code
I used is printed below the following results. Anybody have any idea of
what is going on?
2.05 (1)
roundit: 2.0 ??
2.15 (1)
roundit: 2.1 ??
2.25 (1)
roundit: 2.3 OK
2.35 (1)
roundit: 2.4 OK
2.45 (1)
roundit: 2.5 OK
2.55 (1)
roundit: 2.5 ??
2.65 (1)
roundit: 2.6 ??
2.75 (1)
roundit: 2.8 OK
2.85 (1)
roundit: 2.9 ??
2.95 (1)
roundit: 3.0 OK
======================================
use warnings;
@nums = qw
(
2.05 1
2.15 1
2.25 1
2.35 1
2.45 1
2.55 1
2.65 1
2.75 1
2.85 1
2.95 1
);
for ($i = 0; $i < $#nums; $i += 2)
{
$num = $nums[$i];
$places = $nums[$i+1];
print "$num ($places)\n";
print " roundit: ", roundit($num, $places), "\n\n";
}
#--------------------------------------
sub roundit
{
my($num, $places) = @_;
return sprintf "%0.*f", $places, $num;
}
Barry Brevik
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs