>>> "{}".format(1241.123124)
'1241.123124'
>>> "{:g}".format(1241.123124)
'1241.12'
在此处的文档中:https://docs.python.org/2/library/string.html
它说
None The same as 'g'.
最佳答案
这是文档错误。有一个 open bug report on the Python bug tracker .让我们希望它得到解决:
The documentation for a None (empty) format for floats indicates that it is equivalent to the g format. This does not appear to be correct (Precise definition of float string formatting?).
The Python 3.4 documentation (https://docs.python.org/3.4/library/string.html#format-specification-mini-language) seems to be much closer to what Python 2.7 does.
It would be useful to have a more correct documentation for the effect of a None format for floats in Python 2.7 (maybe by copying the Python 3.4 documentation if it applies).
关于python - 为什么 "{}".format(1241.123124) 不同于 "{:g}".format(1241.123124),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25015967/