Popcorn Dave has asked for the wisdom of the Perl Monks concerning the following question:
I'm in the process of writing a stock tracking program that uses sprintf to display the percent change in a given stock to 3 decimal places. However when I use sprintf "%.3f", if my number is 0.010, I'm getting the trailing 0 like I expect since I've set it to 3 decimal places.
What I'd like to do is to get rid of trailing zeros. I've looked over the docs on sprintf and can't find a way to do it. My solution at present is the following:
$percent_change = sprintf "%.3f", $percent_change; $percent_change =~ s/0+$// if $percent_change =~ /0+$/;
That code works, but it seems a bit of a workaround. Have I missed something somewhere in my use of sprintf?
There is no emoticon for what I'm feeling now.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Deleting trailing 0's after using sprintf
by davido (Cardinal) on Mar 19, 2004 at 07:20 UTC | |
|
Re: Deleting trailing 0's after using sprintf
by jweed (Chaplain) on Mar 19, 2004 at 06:39 UTC | |
|
Re: Deleting trailing 0's after using sprintf
by kvale (Monsignor) on Mar 19, 2004 at 06:41 UTC | |
|
Re: Deleting trailing 0's after using sprintf
by BUU (Prior) on Mar 19, 2004 at 06:33 UTC | |
by Popcorn Dave (Abbot) on Mar 20, 2004 at 03:16 UTC | |
|
Re: Deleting trailing 0's after using sprintf
by cLive ;-) (Prior) on Mar 19, 2004 at 06:59 UTC |