The default behaviour for routines like this is to round down to the nearest number, much like int() does for integers. As a result, rarely does any rounding occur in any Perl or C routines on which Perl is based, such as sprintf.

I don't think this is true, tadman. Testing:

% perl -e 'printf "%1.2f\n", 1.234' 1.23
and
% perl -e 'printf "%1.2f\n", 1.235' 1.24

Which gives me to think that rounding does in principle occur in sprintf and printf. The problem is the floating point representation of base-10 numbers, which occasionally gives you less precision than you though you had. If you really need that precision, hunt around for further discussion on the issue using Super Search, and you'll find plenty.

Update: or just read japhy's response on the original thread.



If God had meant us to fly, he would *never* have give us the railroads.
    --Michael Flanders


In reply to Re: Re: Rounding problem with sprintf?? by ChemBoy
in thread Rounding problem with sprintf?? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.