Author mmokrejs
Recipients mmokrejs
Date 2013-02-14.23:47:38
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1360885658.67.0.591874528903.issue17207@psf.upfronthosting.co.za>
In-reply-to
Content
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
History
Date User Action Args
2013-02-14 23:47:38mmokrejssetrecipients: + mmokrejs
2013-02-14 23:47:38mmokrejssetmessageid: <1360885658.67.0.591874528903.issue17207@psf.upfronthosting.co.za>
2013-02-14 23:47:38mmokrejslinkissue17207 messages
2013-02-14 23:47:38mmokrejscreate