in reply to Rounding problem with sprintf??

Update:
Just look down at what ChemBoy wrote and forget I ever said anything. Must be one of those days.

Replies are listed 'Best First'.
Re: Re: Rounding problem with sprintf??
by ChemBoy (Priest) on Jul 03, 2001 at 00:50 UTC
    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