Perl help

Erik Price eprice at ptc.com
Tue Jul 22 16:36:34 EDT 2003


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:
> 
> egon at trogdor:~$ perl -e 'print sprintf( "%.2f\n", 0.562 )'
> 0.56
> egon at trogdor: perl -e 'print sprintf( "%.2f\n", 0.567 )'
> 0.57
> 
> However, I'm extremely confused by the following:
> 
> egon at trogdor:~$ 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




Erik




More information about the gnhlug-discuss mailing list