Click here to get back home

printf: zero pad after the decimal a given amount

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
printf: zero pad after the decimal a given amount jidanni 03-30-2008
Get Chitika Premium
Posted by Frank Seitz on March 31, 2008, 2:12 pm
Please log in for more thread options
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

Posted by Dr.Ruud on March 31, 2008, 3:22 pm
Please log in for more thread options
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."


Posted by Frank Seitz on March 31, 2008, 3:57 pm
Please log in for more thread options
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

Posted by Dr.Ruud on April 1, 2008, 10:41 pm
Please log in for more thread options
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."


Posted by xhoster on March 31, 2008, 4:10 pm
Please log in for more thread options
jidanni@jidanni.org wrote:
> Why is there no way to tell printf to zero pad like the right column:

One reason is that what you want is ill-defined. If we are going to tweak
sprintf to make it suit our personal preferences, I'd rather see a
conversion character that behaved just like %f if given a good number, but
returned the empty string if given either an empty string or undef (rather
than converting it to zero and then applying %f to the zero.)

> 0.1 :0.100
> 0.05 :0.050
> 0.03 :0.030
> 0.025 :0.025
> 0.02 :0.020
> 0.015 :0.015

Apparently you want to preserve non-zero digits even if that means going
beyond 3 digits right of the decimal. But why did you stop at 4?

0.014999999999999999444888

> 0.0125 :0.0125


0.0125000000000000006938893

How many consecutive zeros or nines are needed before you decide there are
enough to ignore what is the right of them?

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Similar ThreadsPosted
How to change decimal point ',' to decimal comma ',' April 13, 2007, 8:09 am
Re: printf doesn't do right justify March 9, 2008, 8:32 am
printf doesn't do right justify March 9, 2008, 8:04 am
printf and variable length? October 25, 2005, 10:03 am
Using printf to modify scalars May 19, 2006, 8:44 am
Can't find right printf format April 28, 2008, 1:45 pm
printf + escape code May 22, 2008, 12:36 am
perl win32 & output of printf November 5, 2004, 4:15 pm
To store the output of printf into a variable... April 3, 2005, 9:56 am
Reducing amount of repetative code October 27, 2004, 4:54 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap