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
Posted by jidanni on March 30, 2008, 4:09 pm
Please log in for more thread options
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;
}

Posted by John W. Krahn on March 31, 2008, 1:24 am
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:
> 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

Posted by szr on March 31, 2008, 4:23 am
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:
> 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;
> }

use constant S => 100000;
for ( 10000, 5000, 3000, 2500, 2000, 1500, 1250, 1000, 900, 625, 500 ) {
printf "%-7g:%01d.%3.3s%s\n", $_ / S, int $_ / S,
sprintf("%05d", $_),
map { $_ ? $_ : '' } ($_ % 100) =~ m!^(\d+?)0*$!;
}

__OUTPUT__
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

:-)

--
szr



Posted by Paul Lalli on March 31, 2008, 1:37 pm
Please log in for more thread options
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

Posted by szr on March 31, 2008, 2:05 pm
Please log in for more thread options
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



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