Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I want to format a real number to 2.4f format.
This is simple one would say just use $n=sprintf("%2.4f",$n); ,
where $n is the number to be rounded. However when I do
this I get a number like 23.4600 or 23.0000, which is indeed
%2.4f format but I want to eliminate the trailing zeros and decimal
where needed resulting in 23.46 and 23
This is a chunk of the code that does it:
$n=sprintf("%2.4f",$n); $n =~ s/0+$//; $n =~ s/\.$//;
This does work just fine, however I would like to find away to do the substitution by combining the these 2 separate substitutions into 1 more eloquant one (single line).... or perhaps an even better way to do it all together. Thank you for your help.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Eliminating Trailing Zeros
by jmcnamara (Monsignor) on Jul 17, 2003 at 15:18 UTC | |
Re: Eliminating Trailing Zeros
by dreadpiratepeter (Priest) on Jul 17, 2003 at 14:29 UTC | |
by Trimbach (Curate) on Jul 17, 2003 at 14:37 UTC | |
| |
by Anonymous Monk on Jul 17, 2003 at 14:40 UTC | |
Re: Eliminating Trailing Zeros
by Aristotle (Chancellor) on Jul 17, 2003 at 15:15 UTC | |
Re: Eliminating Trailing Zeros
by ihb (Deacon) on Jul 17, 2003 at 20:16 UTC | |
by Aristotle (Chancellor) on Jul 23, 2003 at 00:33 UTC | |
Re: Eliminating Trailing Zeros
by Lachesis (Friar) on Jul 17, 2003 at 14:36 UTC | |
by Anonymous Monk on Jul 17, 2003 at 15:06 UTC | |
by jeffa (Bishop) on Jul 17, 2003 at 15:19 UTC | |
by pemungkah (Priest) on Jul 17, 2003 at 20:06 UTC | |
by dragonchild (Archbishop) on Jul 17, 2003 at 20:14 UTC | |
by dreadpiratepeter (Priest) on Jul 17, 2003 at 14:39 UTC | |
Re: Eliminating Trailing Zeros
by demerphq (Chancellor) on Jul 17, 2003 at 15:04 UTC | |
Re: Eliminating Trailing Zeros
by RollyGuy (Chaplain) on Jul 17, 2003 at 14:40 UTC |
Back to
Seekers of Perl Wisdom