Not_a_Number has asked for the wisdom of the Perl Monks concerning the following question:
Can anyone shed any light on this (I find the documentation for printf and sprintf to be particularly vague...):
for my $num ( 0 .. 101 ) { $num += 0.005; printf "%.2f\n", $num; }
Output: I get (using ASP 5.61, WinXP):
0.01 1.00 2.00 3.00 .. 8.01 ..
followed by seemingly random series of x.00 or x.01. Interestingly(?), if I change the number of decimals that I want to round to:
for my $num ( 0 .. 101 ) { $num += 0.0005; my $result = sprintf "%.3f", $num; print "$result\n"; }
I get similarly strange results, but not for the same 'numbers' (eg I get 2.001 and 3.001, as compared with 2.00 and 3.00 in the first case
Am I missing something (as usual)?
tia, dave
Back to
Seekers of Perl Wisdom