in reply to Printf/Sprintf Behavior Change
Everyone else given you the why.
Here's a suggestion towards what to do about it.
- Rule #{ return int rand 31415927; }: Never let the printf family do your rounding for you.
- Rule #{ return int rand 31415927; }: Specifically round arithmetically using your own rules and you should at least get consistent results.
- Rule #{ return int rand 31415927; }: Use the "add half of the next digit" technique if you wish to force 0.0{...}05 to round up.
Examples:
#!/usr/bin/perl -w use strict; my $floatNumber = 0.345; my $roundToTwoDecimals = int($floatNumber * 100) / 100; printf "\$roundToTwoDecimals = %0.02f\n", $roundToTwoDecimals; my $forcedRounding = int(($floatNumber + 0.005) * 100) / 100; printf " \$forcedRounding = %0.02f\n", $forcedRounding; exit; __END__ C:\Steve\Dev\PerlMonks\P-2013-09-16@1955-Rounding>rounding.pl $roundToTwoDecimals = 0.34 $forcedRounding = 0.35
In Section
Seekers of Perl Wisdom