EchoAngel has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, i have this variable set equal to 1.25000000. what can i do to always give 1.25 without the ending zeros. ie 4.60 should get 4.6 ie 8.0990010000 should get 8.099001 note: sometimes this variable has value 25 and should remain 25

Replies are listed 'Best First'.
Re: Formatting numbers
by Joost (Canon) on Jul 20, 2004 at 17:56 UTC
Re: Formatting numbers
by davido (Cardinal) on Jul 20, 2004 at 17:54 UTC
    You could use a RE, which may not be the best approach. But here it is anyway, and it seems to work great. :)

    use strict; use warnings; while ( <DATA> ) { chomp; print "$_ => "; s/(?<=\.)(.+?)0+$/$1/; print "$_\n"; } __DATA__ 1.25000000 4.60 8.0990010000 25

    Much ado about nothing though, strings can be numified just by using them as numbers.


    Dave