classification
Title: string format precision misbehaving
Type: behavior Stage: resolved
Components: ctypes Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: mmokrejs, r.david.murray
Priority: normal Keywords:

Created on 2013-02-14 23:47 by mmokrejs, last changed 2013-02-15 00:16 by r.david.murray. This issue is now closed.

Messages (2)
msg182125 - (view) Author: Martin Mokrejs (mmokrejs) Date: 2013-02-14 23:47
Hi,
  I don't know if this is related to issue8040 or not. I find the 2.7 string formatting behavior inconsistent. I found out sometimes I have to divide my number by 100 so that the percentage values get printed correctly. Somehow, when a percent sign appears in the formatting "definition" python magically multiplies the number by 100. But sometimes not. This is not specified in http://docs.python.org/2/library/stdtypes.html#string-formatting so I really do not like this whole thing, sorry to say that.

Some examples, which should have been posted in the Library reference doc above.

$ python
Python 2.7.3 (default, Dec 19 2012, 00:02:12) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> "%s" % 12.7666666667
'12.7666666667'
>>> "%s" % '{:.2%}'.format(12.7666666667)
'1276.67%'
>>> "%s" % '{:.2%}'.format(float(12.7666666667))
'1276.67%'
>>>
>>> "%s" % ( '{:.4%}'.format(12.7666666667) )
'1276.6667%'
>>> "%s" % ( '{:.4}'.format(12.7666666667) )
'12.77'
>>> "%s" % ( '{:.2}'.format(12.7666666667) )
'1.3e+01'
>>> "%s%%" % ( '{:.2}'.format(12.7666666667) )
'1.3e+01%'
>>> "%s%%" % ( '{:.2}'.format('12.7666666667') )
'12%'
>>> "%s%%" % ( '{:.4}'.format(float(12.7666666667)) )
'12.77%'
>>>

>>> "%s" % ( '{:.2%}'.format(1/10) )
'0.00%'
>>> "%s" % ( '{:.2%}'.format(1/10.0) )
'10.00%'
>>> "%s" % ( '{:.2%}'.format(1/10.0 * 100) )
'1000.00%'
>>> "%s" % ( '{:.2%}'.format((1/10.0) * 100) )
'1000.00%'
>>>
>>> "%s" % ( '{:.2}'.format((1/10.0) * 100) )
'1e+01'
>>> "%s" % ( '{:.2%}'.format((1/10.0) * 100) )
'1000.00%'
>>>


1) Would somebody please document the behavior?

2) Would somebody explain how can I print 12.67% (I want precision of 2 places after the decimal dot).

3) Why sometimes using float() fixes the behavior (likely converting int() to float()?

4) But why does the scientific exponential notation sometimes come out?



5) Finally, it would be nice if the python 2.7 docs contained how to format floats to 2 places after the dot using the "old" syntax":

"float=%f" % (12.7666666667)

I would like to get just "12.77" out. I can use round() but that is not what I am asking for. I want to change just the formatting of the output:

>>> round(12.7666666667, 2)
12.77
>>> 


Thank you
msg182126 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-02-15 00:16
You appear to be mixing up % style formatting and 'format' style formatting, especially since you seem to be using both in your examples, which is redundant.

Please see http://docs.python.org/2.7/library/string.html#format-string-syntax for the explanation of the format method, including the % presentation type.

Your questions will probably be answered better and more thoroughly if you post to the python-lits mailing list (the tracker is not a place to get help, I'm afraid), but some quick hints:  In Python2.7, 1/10 is 0. In a 'format' format string you can get what it sounds like you want by doing this:

  >>> '{:.2f}%'.format(12.67777)
  '12.68%'

In % style formatting you would do

  >>> '%.2f%%' % 12.67777
  '12.68%'

This is all well documented, but if you can see places it could be clarified, please let us know.
History
Date User Action Args
2013-02-15 00:16:50r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg182126

resolution: not a bug
stage: resolved
2013-02-14 23:47:38mmokrejscreate