Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Using (s)printf()

by Anonymous Monk
on Aug 31, 2001 at 16:05 UTC ( #109360=note: print w/replies, xml ) Need Help??

in reply to Using (s)printf()

I tried to use the example given in the text. This is what I get (using perl v5.6.1 built for cygwin):      printf("%.2f", 9.333333);

This prints "9.33". Actually, this is what I expect, but the article says it should be 9.34.

If I try the following:      printf("%.1f", 9.55);

I get 9.6, which is OK, but if I try      printf("%.1f", 43.55);

I get 43.5, which is rounded the wrong way.

Can someone explain this? (Please send me an email (

Replies are listed 'Best First'.
Re: Re: Using (s)printf()
by Hofmator (Curate) on Aug 31, 2001 at 16:20 UTC

    • The first one (9.3333 -> 9.34) is a typo in the original.
    • The second and third one are easily explained by the following
      printf("%.15f", 9.55); # prints 9.550000000000001 printf("%.15f", 43.55); # prints 43.549999999999997
      This means that due to the limitations of the internal representation, the literal 9.55 is in fact 9.550000000000001 which rounds to 9.56. The same goes for 43.55 but there the representation is slightly smaller than 43.55 so a rounding down occurs.

    -- Hofmator

      Actually, this isn't quite the whole story. Most everyone follows the IEEE convention of "round towards nearest or even."
      Examples (rounding all of these to the one's place):

      2.51 becomes 3 (this is the 'nearest' rule, which always comes first)
      2.49 becomes 2 (again, 'nearest')

      However, what happens if you have 2.50 ? Which way do you round it... 'tis no nearer to 2 than to 3. The IEEE standard says if there is a tie, round to the even number.

      2.50 becomes 2
      3.50 becomes 4

      You have to pick up or down... this method is consistent and thus tends to make your errors (statistically) smaller.

        Actually, this isn't quite the whole story. Most everyone follows the IEEE convention of "round towards nearest or even."
        I'm aware of that - but it wasn't applicable in this case. Btw, (s)printf doesn't seem to get it right:
        printf "%.15f", 0.5; # prints 0.500000000000000 printf "%.0f", 0.5; # prints 1 and should print 0
        and 0.5 can be represented exactly ...

        -- Hofmator

      Thank you for your explanation. How do I circumvent this problem? I have a little perl-program that pulls these numbers from a file and it has to round them correctly. Isn't there some way to store a number exactly?

        What if you want to chop a number but not round it? printf just *wants* to round, no option to tell it not to :(

        I needed something that would chop the extra digits (price calculataions) without rounding.

        quick fox was to compare the sprintf result with the original and if it was larger remove 0.01 - yuk!...

        I ended up using split and substr... - not pretty either

        There must be a better way!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://109360]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2018-06-20 08:10 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.