I think the safest way to round is to add 0.5, then use the
POSIX::floor function. The Perl docs warn against using int() for
just about anything (I don't have the exact quote handy):
use strict;
use POSIX qw(floor);
MAIN: {
foreach my $x (
0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0,
1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5,
0.4999999, 0.511111,
1.4999999, 1.511111,
2.4999999, 2.511111,
) {
my $y = round($x);
print("$x => $y\n");
}
} # MAIN()
# ----------------------------------------------------------------
sub round { my($x) = @_;
return floor($x + 0.5);
} # round()
OUTPUT:
0 => 0
0.1 => 0
0.2 => 0
0.3 => 0
0.4 => 0
0.5 => 1
0.6 => 1
0.7 => 1
0.8 => 1
0.9 => 1
1 => 1
1.5 => 2
2.5 => 3
3.5 => 4
4.5 => 5
5.5 => 6
6.5 => 7
7.5 => 8
8.5 => 9
9.5 => 10
10.5 => 11
0.4999999 => 0
0.511111 => 1
1.4999999 => 1
1.511111 => 2
2.4999999 => 2
2.511111 => 3
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs