sprintf doesn't round correctly?

Discussion in 'Perl Misc' started by void.no.spam.com@gmail.com, Sep 27, 2008.

  1. Guest

    Hi, I have the following code, where the multiplication results in a
    dollar amount which needs to be rounded to the nearest cent.

    $a = sprintf("%.2f", 1900*18.12895);
    print $a;

    The multiplication yields the value 34445.005, which should be rounded
    up to 34445.01, but perl says that $a has the value 34445.00. Why is
    it exhibiting this behavior?
    , Sep 27, 2008
    #1
    1. Advertising

  2. schrieb:
    > Hi, I have the following code, where the multiplication results in a
    > dollar amount which needs to be rounded to the nearest cent.
    >
    > $a = sprintf("%.2f", 1900*18.12895);
    > print $a;
    >
    > The multiplication yields the value 34445.005, which should be rounded
    > up to 34445.01, but perl says that $a has the value 34445.00. Why is
    > it exhibiting this behavior?


    sprintf doesn't round at all. In your example, you get the result of
    your multiplication, and after that, sprintf gives you the result with
    the two digits after the ".".
    More about rounding in Perl:
    http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
    Chris Haffenstedt, Sep 27, 2008
    #2
    1. Advertising

  3. Guest

    On Sep 27, 3:38 pm, Chris Haffenstedt <> wrote:
    > schrieb:
    >
    > > Hi, I have the following code, where the multiplication results in a
    > > dollar amount which needs to be rounded to the nearest cent.

    >
    > > $a = sprintf("%.2f", 1900*18.12895);
    > > print $a;

    >
    > > The multiplication yields the value 34445.005, which should be rounded
    > > up to 34445.01, but perl says that $a has the value 34445.00.  Why is
    > > it exhibiting this behavior?

    >
    > sprintf doesn't round at all. In your example, you get the result of
    > your multiplication, and after that, sprintf gives you the result with
    > the two digits after the ".".
    > More about rounding in Perl:http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html


    That is not true, because I tried this code:

    $a = sprintf("%.2f", 3.0051);
    print $a;

    And it printed out 3.01
    , Sep 27, 2008
    #3
  4. "" <> wrote:
    >Hi, I have the following code, where the multiplication results in a
    >dollar amount which needs to be rounded to the nearest cent.
    >
    >$a = sprintf("%.2f", 1900*18.12895);
    >print $a;
    >
    >The multiplication yields the value 34445.005,


    No, it doesn't. At least not on a typical computer.

    >which should be rounded
    >up to 34445.01, but perl says that $a has the value 34445.00. Why is
    >it exhibiting this behavior?


    You must have missed the introductory lesson in the Introduction into
    Computer Numerics class.

    Try
    $a = sprintf("%.20f", 1900*18.12895);
    print $a;
    and the reason will become obvious (note: 2 replaced with 20 digits).

    For a more detailed explanation please see 'perldoc -q 9999'.

    jue
    Jürgen Exner, Sep 27, 2008
    #4
  5. "" <> writes:

    > Hi, I have the following code, where the multiplication results in a
    > dollar amount which needs to be rounded to the nearest cent.
    >
    > $a = sprintf("%.2f", 1900*18.12895);
    > print $a;
    >
    > The multiplication yields the value 34445.005, which should be rounded
    > up to 34445.01, but perl says that $a has the value 34445.00. Why is
    > it exhibiting this behavior?


    The direction of rounding at exactly x.yx5 is debatable. Perl uses
    banker's rounding, which rounds towards even:

    printf("%1.0f",0.5) => "0"
    printf("%1.0f",1.5) => "2"

    There are good reasons why it does this for general rounding: the last
    time we had this question it turned into a rediculously long discussion.

    Just see http://en.wikipedia.org/wiki/Rounding

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
    Joost Diepenmaat, Sep 27, 2008
    #5
  6. Guest

    On Sep 27, 3:46 pm, Jürgen Exner <> wrote:
    > "" <> wrote:
    > >Hi, I have the following code, where the multiplication results in a
    > >dollar amount which needs to be rounded to the nearest cent.

    >
    > >$a = sprintf("%.2f", 1900*18.12895);
    > >print $a;

    >
    > >The multiplication yields the value 34445.005,

    >
    > No, it doesn't. At least not on a typical computer.
    >
    > >which should be rounded
    > >up to 34445.01, but perl says that $a has the value 34445.00.  Why is
    > >it exhibiting this behavior?

    >
    > You must have missed the introductory lesson in the Introduction into
    > Computer Numerics class.
    >
    > Try
    >         $a = sprintf("%.20f", 1900*18.12895);
    >         print $a;
    > and the reason will become obvious (note: 2 replaced with 20 digits).
    >
    > For a more detailed explanation please see 'perldoc -q 9999'.
    >
    > jue


    Ah, that explains it. Thanks.
    , Sep 27, 2008
    #6
  7. Joost Diepenmaat <> wrote:
    >"" <> writes:
    >
    >> Hi, I have the following code, where the multiplication results in a
    >> dollar amount which needs to be rounded to the nearest cent.
    >>
    >> $a = sprintf("%.2f", 1900*18.12895);
    >> print $a;
    >>
    >> The multiplication yields the value 34445.005, which should be rounded
    >> up to 34445.01, but perl says that $a has the value 34445.00. Why is
    >> it exhibiting this behavior?

    >
    >The direction of rounding at exactly x.yx5 is debatable. Perl uses
    >banker's rounding, which rounds towards even:


    This may or may not be the case. Either way it is not the reason for the
    OPs problem. He doesn't have a trailing '5' do being with.

    jue
    Jürgen Exner, Sep 27, 2008
    #7
  8. Tim Greer Guest

    wrote:

    > Hi, I have the following code, where the multiplication results in a
    > dollar amount which needs to be rounded to the nearest cent.
    >
    > $a = sprintf("%.2f", 1900*18.12895);
    > print $a;
    >
    > The multiplication yields the value 34445.005, which should be rounded
    > up to 34445.01, but perl says that $a has the value 34445.00. Why is
    > it exhibiting this behavior?


    See:

    perldoc -q decimals
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
    Tim Greer, Sep 27, 2008
    #8
  9. On Sat, 27 Sep 2008 21:38:26 +0200,
    Chris Haffenstedt <> wrote:
    > schrieb:
    >> Hi, I have the following code, where the multiplication results in a
    >> dollar amount which needs to be rounded to the nearest cent.
    >>
    >> $a = sprintf("%.2f", 1900*18.12895);
    >> print $a;

    >
    > sprintf doesn't round at all.


    sprintf and printf do rounding.

    See

    # perldoc -q round

    and

    # perldoc -f sprintf
    (search for 'rounded')

    Martien
    --
    |
    Martien Verbruggen | "In a world without fences,
    | who needs Gates?"
    |
    Martien Verbruggen, Sep 27, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    434
    =?Utf-8?B?TWF0SGVydGVs?=
    Oct 8, 2005
  2. Replies:
    0
    Views:
    1,070
  3. Yodai
    Replies:
    2
    Views:
    438
    Alex Monjushko
    Jan 14, 2004
  4. OliverMarchand

    Float.round - should it be round-to-even

    OliverMarchand, Apr 12, 2006, in forum: Ruby
    Replies:
    2
    Views:
    209
    OliverMarchand
    Apr 12, 2006
  5. cerr

    reading file round and round

    cerr, Mar 19, 2010, in forum: Perl Misc
    Replies:
    6
    Views:
    169
    Peter J. Holzer
    Mar 20, 2010
Loading...

Share This Page