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

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

am 31.03.2008 21:22:59 von rvtol+news

jidanni@jidanni.org schreef:
> 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

$ perl -wle'
print "".reverse sprintf "%05.1f", "".reverse sprintf "%f", $_
for qw/.1 .05 .03 .025 .02 .015 .0125 .01 .009 .00625 .005
1.987654321E1/
'
0.100
0.050
0.030
0.025
0.020
0.015
0.0125
0.010
0.009
0.00625
0.005
9.876543

;)

--
Affijn, Ruud

"Gewoon is een tijger."

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

am 31.03.2008 21:57:23 von Frank Seitz

Dr.Ruud wrote:
>
> $ perl -wle'
> print "".reverse sprintf "%05.1f", "".reverse sprintf "%f", $_
> for qw/.1 .05 .03 .025 .02 .015 .0125 .01 .009 .00625 .005
> 1.987654321E1/
> '
> 0.100
> 0.050
> 0.030
> 0.025
> 0.020
> 0.015
> 0.0125
> 0.010
> 0.009
> 0.00625
> 0.005
> 9.876543
>
> ;)

And how do you deal with negative numbers and numbers >= 10? ;)

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

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

am 02.04.2008 04:41:28 von rvtol+news

Frank Seitz schreef:
> Dr.Ruud:

>> $ perl -wle'
>> print "".reverse sprintf "%05.1f", "".reverse sprintf "%f", $_
>> for qw/.1 .05 .03 .025 .02 .015 .0125 .01 .009 .00625 .005
>> 1.987654321E1/
>> '
>> 0.100
>> [...]
>> 9.876543
>>
>> ;)
>
> And how do you deal with negative numbers and numbers >= 10? ;)

Hey! There were only numbers in [0..1>, so I already extended it to
[0..10>.

Oh well,

perl -wle'
$n=length(int abs),
print+($_<0?"-":"").reverse sprintf"%0*.*f",$n+4,$n,
"".reverse sprintf"%f",abs
for qw/0 .1 .05 .03 .025 .02 .015 .0125 .01 .009 .00625 .005
1234567.89 -9876543.21/
'
0.000
0.100
0.050
0.030
0.025
0.020
0.015
0.0125
0.010
0.009
0.00625
0.005
1234567.890
-9876543.210

--
Affijn, Ruud

"Gewoon is een tijger."