Title: String formatting: incorrect number of decimal places
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.4
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: cjwelborn, docs@python, eric.smith, jluscher
Priority: normal Keywords:

Created on 2015-05-25 03:47 by jluscher, last changed 2015-05-26 13:24 by jluscher. This issue is now closed.

Messages (5)
msg244011 - (view) Author: James Luscher (jluscher) Date: 2015-05-25 03:47
Doc for 3.4: at Format Specification Mini-Language
indicates that:
"The precision is a decimal number indicating how many digits should be displayed after the decimal point for a floating point value"

Yet I find that I get this behavior:

Python 3.4.3 (default, Mar 26 2015, 22:03:40) 
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> '{:08.3}'.format(12.34)

When what I am expecting is:  '0012.340'
msg244012 - (view) Author: Christopher Welborn (cjwelborn) * Date: 2015-05-25 04:02
You forgot the 'f' to specify that you want float formatting:

>>> '{:08.3f}'.format(12.34)

msg244045 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2015-05-25 18:40
Just to clarify:

If not specified, the default "presentation type" for floats is "g". Since you didn't specify "f" in your example, it's using "g". For presentation type "g", the precision (3 in your case) is the total number of significant digits. So 12.34 first becomes 12.3, then that's displayed in a field 8 characters wide, which gives you the 4 leading zeros.

See the section of presentation types for floating point at the end of this section:
msg244096 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2015-05-26 11:03
And I just double checked: the entirety of the sentence you quoted is:

The precision is a decimal number indicating how many digits should be displayed after the decimal point for a floating point value formatted with 'f' and 'F', or before and after the decimal point for a floating point value formatted with 'g' or 'G'.

I think that's reasonably clear for 'g' and 'G', but if you think it could be improved, I'm open to suggestions. Maybe the disconnect is that 'g' is the default?

The only reason I'm following up on this is that I'm familiar with all of this, yet it still took me some experimenting to remember what the issue is.
msg244102 - (view) Author: James Luscher (jluscher) Date: 2015-05-26 13:24
I am not familiar with the 'g' format and did not know it was the default, but the documentation, read fully, is correct.  It just took the response of Christopher Welborn to "wake me up" (it was a LONG day and I was "struggling" to understand the problem - I would hate to tell you how long I worked on this problem !  {embarrassment!!}).

My programming usually involves text manipulation, data structures and GUIs.  The am familiar with the use of 'precision' as length of the decimal fraction but was thrown by (the equally valid) use of 'precision' as the mathematical idea referring to 'significant digits'. Counting BOTH sides of decimal point was a difficult switch of word meaning to get my head around.

Just because "I" have never(!) used the 'g' format doesn't mean it isn't the proper choice for 'default'.  Language is often a challenge and 'precision' with two meanings (# of digits to right of decimal AND total # of digits {right and left of decimal)) does present a linguistic challenge to one's ability to comprehend what the word means ;-)

I wish I could give you the solution but at least I (finally !) understand the mental issue involved.

Thanks to both of you for your help getting me past my 'mental fugue' ;-)
Date User Action Args
2015-05-26 13:24:10jluschersetmessages: + msg244102
2015-05-26 11:03:22eric.smithsetmessages: + msg244096
2015-05-25 18:40:43eric.smithsetmessages: + msg244045
2015-05-25 04:27:09r.david.murraysetstatus: open -> closed
resolution: not a bug
stage: resolved
2015-05-25 04:02:57cjwelbornsetnosy: + cjwelborn
messages: + msg244012
2015-05-25 03:53:51larrysetnosy: + eric.smith
2015-05-25 03:47:41jluschercreate