tphyahoo has asked for the wisdom of the Perl Monks concerning the following question:
Currently I'm doing it as below, which works for 33% but not for 3% (extra zero at the beginning, i.e. 03%). Is there a more elegant/intuitive way to do this than just using sprintf?
Thanks!
use strict; use warnings; use Test::More qw(no_plan); my $one_third = 1/3; my $percent = sprintf("%02d%%",$one_third*100); is($percent,"33%"); print "Result: $percent\n"; my $one_thirtieth = 1/30; $percent = sprintf("%02d%%",$one_thirtieth*100); is($percent,"3%"); print "Result: $percent\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: sprintf percent formatting
by tbone1 (Monsignor) on Aug 17, 2005 at 12:50 UTC | |
|
Re: sprintf percent formatting
by b10m (Vicar) on Aug 17, 2005 at 12:50 UTC | |
|
Re: sprintf percent formatting
by derby (Abbot) on Aug 17, 2005 at 12:49 UTC | |
by xdg (Monsignor) on Aug 17, 2005 at 13:14 UTC | |
by Roger (Parson) on Aug 17, 2005 at 12:59 UTC | |
by tphyahoo (Vicar) on Aug 17, 2005 at 13:14 UTC | |
by Util (Priest) on Aug 17, 2005 at 14:44 UTC |