in reply to Proper monetary rounding

Here's a little toy to examine just what's going on here. Try it out and play with the ranges.
#!/usr/bin/perl -w use strict; require Math::BigFloat; my $sig = $ARGV[0] || 2; my $ini = $ARGV[1] || 0; my $end = $ARGV[2] || 1; my $inc = $ARGV[3] || 0.0001; test_methods($sig, $ini, $end, $inc); sub test_methods { my ($sig, $ini, $end, $inc) = @_; my $count = 0; my %result = (); print "For $ini to $end, incrementing by $inc, with $sig significa +nt digit". ($sig==1 ? '' : 's') ."\n"; for (my $num = $ini;$num<$end;$num += $inc) { $count++; $result{'real'} += $num; $result{'ffround'} += Math::BigFloat->new($num)->ffround( -$si +g ); $result{'sprintf'} += sprintf "%0.${sig}f", $num; printf "%-20s || %-10s || %-20s\r", ($result{'real'}/$count), +($result{'ffround'}/$count), ($result{'sprintf'}/$count); } print ' 'x58 . "\rOver $count iterations:\n"; print " Method | Sum | Average\n"; for (qw[real ffround sprintf]) { printf "%9s | %-20s| %-20s\n", $_, $result{$_}, ($result{$_}/$ +count); } return \%result; }

"One is enough. If you are acquainted with the principle, what do you care for the myriad instances and applications?"
- Henry David Thoreau, Walden