in reply to Simple Rounding
As Abigail-II says, sprintf will guarantee two digits after the decimal point. But it won't guarantee consistent rounding. Try this:
my @nums = ( 62, 63, 64, 65 ); for ( @nums ) { $_ += 0.005; printf "%.2f\n", $_; }
Output (at least on my machine):
62.01 63.01 64.00 65.00
To quote Zaxo from the thread (s)printf and rounding woes:
"That is a general property of binary floating-point numbers... If you need consistency in a fixed-point decimal representation, you should scale the numbers to be represented as integers."
dave
|
|---|