470,815 Members | 1,288 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

Undocumented alternate form for %#f ?

I'm assuming this is by-design, but it doesn't appear to be documented:
'%8.f' % (-1) ' -1' '%#8.f' % (-1)

' -1.'
The docs list the alternate forms, but there isn't one listed for f/F. Itwould seem the alternate form for floating points is truncate & round the floating point value, but always display the . at the end. Is that correct?

Do you want to help develop Dynamic languages on CLR? (http://members.microsoft.com/careers...8-DC1B43134038)

Apr 28 '06 #1
1 1670
Dino Viehland wrote:
I'm assuming this is by-design, but it doesn't appear to be
documented:
'%8.f' % (-1) ' -1' '%#8.f' % (-1) ' -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):
"%8f" % (-1) '-1.000000' "%#8f" % (-1)

'-1.000000'

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.
--

Apr 28 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Joshua Beall | last post: by
1 post views Thread by news | last post: by
1 post views Thread by jeffgeorge | last post: by
2 posts views Thread by xmutantduck | last post: by
1 post views Thread by Eric Lindsay | last post: by
4 posts views Thread by =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.