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

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

am 31.03.2008 19:37:14 von Paul Lalli

On Mar 30, 4:09=A0pm, jida...@jidanni.org wrote:
> Why is there no way to tell printf to zero pad like the right column:

Why do you assume that because you don't know the way, there is no
way?

> 0.1 =A0 =A0:0.100
> 0.05 =A0 :0.050
> 0.03 =A0 :0.030
> 0.025 =A0:0.025
> 0.02 =A0 :0.020
> 0.015 =A0:0.015
> 0.0125 :0.0125
> 0.01 =A0 :0.010
> 0.009 =A0:0.009
> 0.00625:0.00625
> 0.005 =A0:0.005
> The challenge: Change only the "WHAT?" below to produce the right
> column above. Thanks.
> use constant S =3D> 100000;
> for ( 10000, 5000, 3000, 2500, 2000, 1500, 1250, 1000, 900, 625, 500 ) {
> =A0 =A0 printf "%-7g:WHAT?\n", $_ / S, $_ / S;
>

%.03f

$ perl -e'printf("%.03f\n", .1)'
0.100

Paul Lalli

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

am 31.03.2008 20:05:51 von szr

Paul Lalli wrote:
> On Mar 30, 4:09 pm, jida...@jidanni.org wrote:
>> Why is there no way to tell printf to zero pad like the right column:
>
> Why do you assume that because you don't know the way, there is no
> way?
>
>> 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;
>>
>
> %.03f
>
> $ perl -e'printf("%.03f\n", .1)'
> 0.100
>
> Paul Lalli

Actually that truncates to 3 decimal places, which isn't what the op
required:

$ perl -e'printf("%.03f\n", .00625)'
0.006


See my other post for a working solution.

--
szr

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

am 31.03.2008 20:12:34 von Frank Seitz

szr wrote:
> Paul Lalli wrote:
>>
>>%.03f
>>
>>$ perl -e'printf("%.03f\n", .1)'
>>0.100
>>
>>Paul Lalli
>
> Actually that truncates to 3 decimal places, which isn't what the op

s/truncates/rounds/;

> required:
>
> $ perl -e'printf("%.03f\n", .00625)'
> 0.006

The 0 in .03 is useless.

Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel