Alex (via RT) wrote: > I try to round 1.045 on my Perl (v5.6.1) but output is very strange: > perl -e "print sprintf('%.2f', '1.045')" > 1.04 > perl -e "print sprintf('%.2f', '1.0451')" > 1.05 > Is it the bug? Nope, it's just the way that sprintf() rounds when the digit after the precision limit is exactly 5. In this case, sprintf always rounds towards zero: $ perl -e "print sprintf('%.2f', '-1.045')" -1.04 I'll grant that this is suprising given the way most kids are taught to round in school. But there are several ways to define rounding, the most "accurate" being "banker's rounding" or "even rounding" where 1.045 => 1.04 and 1.055 => 1.06 which turns out to be (over large numbers of calculations) less biased. Here's a similar thing in C: #include <stdio.h> int main() { printf("%.2f\n",1.045); printf("%.2f\n",1.0451); printf("%.2f\n",1.055); printf("%.2f\n",1.0551); } which outputs: 1.04 1.05 1.05 1.06 HTH John -- John Peacock Director of Information Research and Technology Rowman & Littlefield Publishing Group 4720 Boston Way Lanham, MD 20706 301-459-3366 x.5010 fax 301-429-5747Thread Previous