python - 缺少 str.format() 行为的类型

标签 python printing format

根据 Python 文档 here , 当省略类型时,它默认为浮点参数的“g”类型。

但是,

print("{0:.2}".format(14.9))

打印“1.5e+01”,而

print("{0:.2g}".format(14.9))

打印“15”

这仅仅是文档不正确的问题还是有其他原因?

最佳答案

根据source code ,这是一个文档错误。对于没有 float 说明符的行为的正确描述是“类似于‘g’,但总是在小数点后至少保留一位数字”。

关于python - 缺少 str.format() 行为的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4954074/

相关文章:

python - padding之间的匹配数据

python - 如何在Python中以小端、填充零和无 '0x'的方式打印十六进制数?

Java 8 DecimalFormat 给出不一致的输出

c - 如何在 C 中将 int 打印为二进制?

python - 使用要排除的索引列表进行索引

Python: `1+1j == (1+1j)` 但 `1+1j.imag == 2` ,为什么?

python - 使用python处理内存中的文件

perl - 如何使用 Term::Screen 打印到 STDERR?

javascript - 从硬拷贝中删除打印按钮

ios - 如何从 UIPrintFormatter 获取准确的 pageCount