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 Ben Morrow on March 31, 2008, 4:55 pm
Please log in for more thread options

Quoth xhoster@gmail.com:
> 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?

It appears to me the OP wants either 3 s.f. after the point or 3 places,
whichever comes out shorter. Something like

sub fmt {
return
map /(\d*\.\d\d*?)0*$/,
map /(\d*\.0*[1-9]\d\d)/,
map { sprintf "%.308f", $_ }
@_;
}

appears to work, but it's hardly pretty :(. The 308 is the number of
places required to represent DBL_MIN with 53-bit doubles; if your perl
is using 64-bit long doubles you will need 4932 instead.

Ben


Posted by Ted Zlatanov on April 1, 2008, 9:35 am
Please log in for more thread options

BM> It appears to me the OP wants either 3 s.f. after the point or 3 places,
BM> whichever comes out shorter. Something like

BM> sub fmt {
BM> return
BM> map /(\d*\.\d\d*?)0*$/,
BM> map /(\d*\.0*[1-9]\d\d)/,
BM> map { sprintf "%.308f", $_ }
BM> @_;
BM> }

BM> appears to work, but it's hardly pretty :(. The 308 is the number of
BM> places required to represent DBL_MIN with 53-bit doubles; if your perl
BM> is using 64-bit long doubles you will need 4932 instead.

Is there any harm in always using 4932? I would guess not, except maybe
for wasted CPU cycles.

Ted

Posted by jidanni on April 1, 2008, 10:40 pm
Please log in for more thread options
Hi, it's me, the Original Poster. Thanks for all the replies. Mr.
Krahn employed [perlform - Perl formats], but did not check if he
produced the required results. User srz followed with a map()
solution, but my screensaver kicked in before I could figure it out
staring at it so long :-) The solution employing reverse() indeed blew
up on different input. The remaining map() solutions I will study later.

OK, this gives me what I want in the right column it makes,
and is not obfuscated to me:

use constant S => 10000000;
for (
1000000, 500000, 300000, 250000, 200000,
150000, 125000, 100000, 90000, 62500,
50000, 1.987654321E1, 777700000000, -44, -660000000
)
{
for ( $_ / S ) {
printf "%-7g:", $_;
$_ = sprintf "%f", $_;
/.*\.\d(.*[^0])?/;
print "$&\n";
}
}

OK, it seems like I accept some truncation for tiny numbers.

Mainly what is bothering me about %g and %f is that apparently they
don't make full use of what they could have, if implemented.

> The 0 in .03 is useless.

e.g., %-#05.-#03f or something, whatever that would mean. Or at least
%-05.03f etc. What I'm saying is %f and/or %g are like when you
parents chose the two door sedan instead of the four door model that
it could have been, with all the bells and whistles.

Now if it is appropriate to add more creeping features to %f or %g at
this "late stage" is another question. P.S., %g is particular squishy,
turning into "e" notation if fed caterpillars, etc. Scary.

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