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

标签 python string-formatting

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

相关文章:

python - Google Cloud Storage - 将文件从一个文件夹移动到另一个文件夹 - 使用 Python

.net - 使用 XML 序列化格式化元素/属性值

c# - String.Format C# 每1000个数字之间的间距

java - 如何在Java中编写连续命名的文件?

JavaScript toString 或 join 长数字数组 (SSN)

c# - 在 .Net 中为复数形式附加 's' 的巧妙方法(语法糖)

python - 推特/社交数据挖掘——Ruby 还是 Django?

python - 最速下降吐出不合理的大值

python - Django Nginx Gunicorn = 504 超时

python - Spark : Broadcast variables: It appears that you are attempting to reference SparkContext from a broadcast variable, Action ,或转换