Erik Price said: > > Cole Tuininga wrote: >> Got a perl question for y'all. I rarely have to do anything with >> perl, and I'm sure perl has a good reason for behaving like the >> following, but heck if I can figure it out. >> >> The perl cookbook suggestions using sprintf for rounding floats. This >> seemingly works fine: >> >> [EMAIL PROTECTED]:~$ perl -e 'print sprintf( "%.2f\n", 0.562 )' >> 0.56 >> [EMAIL PROTECTED]: perl -e 'print sprintf( "%.2f\n", 0.567 )' >> 0.57 >> >> However, I'm extremely confused by the following: >> >> [EMAIL PROTECTED]:~$ perl -e 'print sprintf( "%.2f\n", 0.565 )' >> 0.56 > > I don't have an answer, Cole, but the same happens in Python and I'd bet > in C as well -- must be a convention of the printf routines: > > Python 2.2.2 (#1, Mar 9 2003, 08:18:26) > [GCC 3.2 20020927 (prerelease)] on cygwin > Type "help", "copyright", "credits" or "license" for more information. > >>> print "%.2f\n" % 0.562 > 0.56 > > >>> print "%.2f\n" % 0.567 > 0.57 > > >>> print "%.2f\n" % 0.565 > 0.56
It's kind of a kludge, but perhaps adding some very small value such as 0.00001 just prior to the rounding will get the result you want? [EMAIL PROTECTED]:~$ perl -e 'print sprintf( "%.2f\n", 0.56501 )' 0.57 -- Bill Mullen [EMAIL PROTECTED] MA, USA RLU #270075 MDK 8.1 & 9.0 "Giving money and power to the government is like giving whiskey and car keys to teenage boys." - P.J. O'Rourke _______________________________________________ gnhlug-discuss mailing list [EMAIL PROTECTED] http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss