Undocumented alternate form for %#f ?

D

Dino Viehland

D

Dave Hughes

Dino said:
I'm assuming this is by-design, but it doesn't appear to be
documented:

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top