in reply to sprintf on floating point

<wrong>$_ = sprintf('%.f',sprintf('%1.2f', 2e-7))</wrong>

How about that?

UPDATE: sorry, i didn't test this. The '0+' approach is much better.

--
http://fruiture.de

Replies are listed 'Best First'.
Re: Re: sprintf on floating point
by zigdon (Deacon) on Sep 05, 2002 at 14:17 UTC
    perl -e 'print sprintf "%f", sprintf "%0.2f", 0.1' 0
    However, you could do it this way (though without using sprintf):
    perl -e 'print 0 + sprintf "%0.2f", 0.001' 0 perl -e 'print 0 + sprintf "%0.2f", 0.1234' 0.12
    Does that help?

    -- Dan

      It does help however the first one doesn't work.
Re: Re: sprintf on floating point
by Jaap (Curate) on Sep 05, 2002 at 14:16 UTC
    There's one tiny little problem with this: 0.12345 becomes 0 too. That should become 0.12