New submission from Martin Mokrejs: 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 ---------- components: ctypes messages: 182125 nosy: mmokrejs priority: normal severity: normal status: open title: string format precision misbehaving type: behavior versions: Python 2.7 _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue17207> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com