in reply to Re: substitution "0" in decimals
in thread substitution "0" in decimals

Be aware though that forcing numericification does lose precision, as .12 cannot be representated by an exact binary representation:
perl -wE '$d1 = "1230.1200"; $d1 = $d1 + 0; printf "%.20f\n", $d1' 1230.11999999999999999556
It may not matter for the OP, but at least he should be made aware of when using this trick.

Replies are listed 'Best First'.
Re^3: substitution "0" in decimals
by LanX (Saint) on Dec 08, 2009 at 11:57 UTC
    > It may not matter for the OP, but at least he should be made aware of when using this trick.

    see footnotes in Re^3: substitution "0" in decimals

    Anyway I'm not sure if a conversion to float (resulting in information loss) really happens when stored or just during some numerical calculations:

    DB<4> $d1 = "1230.1200"; DB<5> $d1 = $d1 + 0; DB<6> print $d1."00" 1230.1200

    Cheers Rolf

      In happens when converting to floats. Now, when converting floats to strings, Perl only uses a limited (6?) number of digits after the decimal point, if the number before the decimal point isn't 0. In this case (and many others), this cancels out the loss of significance.

      In many cases, you will not notice the difference, Perl will just "do what I mean". But sometimes, you'll be surprised. String to float and back isn't always free.

        I was wondering if it is really stored as a float.

        your explanation with limited number of digits is good ( I was thinking of that), but should include rounding!

        > In many cases, you will not notice the difference, Perl will just "do what I mean". But sometimes, you'll be surprised. String to float and back isn't always free.

        Plz, can you give me an example?

        UPDATE: Never mind, see update to Re: substitution "0" in decimals!

        Cheers Rolf