RE: Undocumented alternate form for %#f ?
- From: Dino Viehland <dinov@xxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 28 Apr 2006 15:48:03 -0700
Ahh, cool... Thanks for the explanation!
Do you want to help develop Dynamic languages on CLR? (http://members.microsoft.com/careers/search/details.aspx?JobID=6D4754DE-11F0-45DF-8B78-DC1B43134038)
-----Original Message-----
From: python-list-bounces+dinov=microsoft.com@xxxxxxxxxx [mailto:python-list-bounces+dinov=microsoft.com@xxxxxxxxxx] On Behalf Of Dave Hughes
Sent: Friday, April 28, 2006 1:00 PM
To: python-list@xxxxxxxxxx
Subject: Re: Undocumented alternate form for %#f ?
Dino Viehland wrote:
I'm assuming this is by-design, but it doesn't appear to be
documented:
' -1''%8.f' % (-1)
' -1.''%#8.f' % (-1)
The docs list the alternate forms, but there isn't one listed for
f/F. It would seem the alternate form for floating points is
truncate & round the floating point value, but always display the .
at the end. Is that correct?
The Python % operator follows the C sprintf function pretty darn
closely in behaviour (hardly surprising really, though I've never
peeked at the implementation). Hence "man sprintf" can provide some
clues here. From man sprintf on my Linux box:
#
The value should be converted to an ``alternate form''. For o
conversions, the first character of the output string is made
zero (by prefixing a 0 if it was not zero already). For x and X
conversions, a non-zero result has the string `0x' (or `0X' for
X conversions) prepended to it. For a, A, e, E, f, F, g, and G
conversions, the result will always contain a decimal point,
even if no digits follow it (normally, a decimal point appears
in the results of those conversions only if a digit follows).
For g and G conversions, trailing zeros are not removed from the
result as they would otherwise be. For other conversions, the
result is undefined.
Hence, I don't think it's the # doing the truncating here, but it
certainly is producing the mandatory decimal point. If you get rid of
the "." in the specification, it uses the default decimal precision (6):
'-1.000000'"%8f" % (-1)
'-1.000000'"%#8f" % (-1)
No difference with the alternate specification here as the precision is
non-zero. Again, from man sprintf:
The precision
[snip]
If the precision is given as
just `.', or the precision is negative, the precision is taken to be
zero. This gives the minimum number of digits to appear for d, i, o,
u, x, and X conversions, the number of digits to appear after the radix
character for a, A, e, E, f, and F conversions, the maximum number of
significant digits for g and G conversions, or the maximum number of
characters to be printed from a string for s and S conversions.
HTH,
Dave.
--
--
http://mail.python.org/mailman/listinfo/python-list
.
- References:
- Re: Undocumented alternate form for %#f ?
- From: Dave Hughes
- Re: Undocumented alternate form for %#f ?
- Prev by Date: Popping from the middle of a deque + deque rotation speed
- Next by Date: iputils module
- Previous by thread: Re: Undocumented alternate form for %#f ?
- Next by thread: Possible constant assignment operators ":=" and "::=" for Python
- Index(es):
Relevant Pages
|