Re: printf: zero pad after the decimal a given amount



jidanni@xxxxxxxxxxx wrote:
Why is there no way to tell printf to zero pad like the right column:
0.1 :0.100
0.05 :0.050
0.03 :0.030
0.025 :0.025
0.02 :0.020
0.015 :0.015
0.0125 :0.0125
0.01 :0.010
0.009 :0.009
0.00625:0.00625
0.005 :0.005
The challenge: Change only the "WHAT?" below to produce the right
column above. Thanks.
use constant S => 100000;
for ( 10000, 5000, 3000, 2500, 2000, 1500, 1250, 1000, 900, 625, 500 ) {
printf "%-7g:WHAT?\n", $_ / S, $_ / S;
}

$ perl -le'
use constant S => 100000;
my $x;
format =
@<<<<<< : @.#####
$x, $x
..
for ( 10000, 5000, 3000, 2500, 2000, 1500, 1250, 1000, 900, 625, 500 ) {
$x = $_ / S;
write;
}
'
0.1 : 0.10000
0.05 : 0.05000
0.03 : 0.03000
0.025 : 0.02500
0.02 : 0.02000
0.015 : 0.01500
0.0125 : 0.01250
0.01 : 0.01000
0.009 : 0.00900
0.00625 : 0.00625
0.005 : 0.00500



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
.



Relevant Pages