>> "{}".format(1241.123124) '1241.123124' >>> "{:g}".format(1241.123124) '1241.12' 在此处的文档中:https://-6ren">
gpt4 book ai didi

python - 为什么 "{}".format(1241.123124) 不同于 "{:g}".format(1241.123124)

转载 作者:太空狗 更新时间:2023-10-30 02:45:20 25 4
gpt4 key购买 nike

>>> "{}".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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com