Re: printf: zero pad after the decimal a given amount
- From: Ben Morrow <ben@xxxxxxxxxxxx>
- Date: Mon, 31 Mar 2008 21:55:16 +0100
Quoth xhoster@xxxxxxxxx:
jidanni@xxxxxxxxxxx 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{3}\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
.
- References:
- printf: zero pad after the decimal a given amount
- From: jidanni
- Re: printf: zero pad after the decimal a given amount
- From: xhoster
- printf: zero pad after the decimal a given amount
- Prev by Date: Re: Windows paths in glob
- Next by Date: every combination of Y/N in 5 positions
- Previous by thread: Re: printf: zero pad after the decimal a given amount
- Next by thread: FAQ 8.2 How come exec() doesn't return?
- Index(es):
Relevant Pages
|