in reply to substitution "0" in decimals

If you really need to assign strings, you will have to force the scalar into a number afterwards
DB<1> $d1 = '1230.1200'; DB<2> print $d1 1230.1200 DB<3> print $d1+0 1230.12 DB<4> $d1=$d1+0 DB<5> print $d1 1230.12

Cheers Rolf

UPDATE: Please be aware about the limitations of floating-point-numbers as illustrated in the folowing example

DB<1> $a='0.0000001'; DB<2> print $a+0 1e-07 DB<3> printf "%s",$a 0.0000001 DB<4> $a=sprintf "%s",$a DB<5> print $a 0.0000001 DB<7> $a='0.00000000000000000000000000000000001000000000000000'; DB<8> $a=sprintf "%s",$a+0 DB<9> print $a 1e-35

If you just wanna reformat a string avoiding any float-representation , you should better consider taking the regex from kennethk's posting!

Replies are listed 'Best First'.
Re^2: substitution "0" in decimals
by Sun751 (Beadle) on Dec 08, 2009 at 00:01 UTC
    Rolf your tricks seems to be easy and very helpful could you please direct me where I can read more about these sort of stuff about perl.
      I don't know which perldoc defines how scalars really work.

      Roughly speaking (my interpretation):

      It's supposed to be transparent if you store a string or a number to a scalar. Operators try an interpretation of operands according to their type.¹

      Storing a number is subject of normalization, since 042.2, 42.20 and 42.2 all represent the same number.²

      But if you store a string by quoting these numbers no information loss is allowed, every zero is a valid character.

      So by reassigning a numeric value ( + is a numerical operator) the number gets normalized.

      Cheers Rolf

      (1) Thats why perl has (at least) 2 flavors for many operators, e.g. eq and == (see Equality Operators) , OTOH Javascript uses + also for concatenation of strings causing many new problems inexistent in perl.

      (2) which may cause new problems, since many dezimal fractions can't be represented as floats w/o a little information loss. (see Re: eq vs ==)

Re^2: substitution "0" in decimals
by JavaFan (Canon) on Dec 08, 2009 at 10:48 UTC
    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.
      > 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.