[Please do not top-post -- it makes the conversation difficult to follow] On Feb 8, John Edwards said:
>>From: Jeff 'japhy' Pinyan [mailto:[EMAIL PROTECTED]] >> >>If you're REALLY worried about whether 12.345 rounds to 12.34 or 12.35, >>then you should use a specific rounding function, but if not, you can use >>sprintf(): >> >> $num = sprintf "%.02f", $raw; > >Ah. Reading this I realised that while the number you gave (24.999997) will >round to 25.00 using sprintf, not all number will round as expected using >that methos. Instead you can use this (which does appear to work). > >$number = "12.345"; # Round this number >$n = 2; # to this many places >$rounded = int($number * (10 ** $n) + .5) / (10 ** $n); >print $rounded; That's the same method I use for my round() function, except I take it one step further and add code that handles negative numbers (which should have ..5 subtracted, not added. int($number * 10**$n + ($number < 0 ? -.5 : .5)) / 10**$n; Someone (either here or on comp.lang.perl.misc) pointed out I could write int($number * 10**$n + .5 * ($number <=> 0)) / 10**$n; -- Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ ** Look for "Regular Expressions in Perl" published by Manning, in 2002 ** <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]