为什么 python 格式函数对某些数字进行四舍五入而不对其他数字进行四舍五入?

Sum*_*nth 5 python string-formatting python-3.x

>>> "{:g}".format(float(214929)/2)                                                                                      
    '107464'  
>>> "{:g}".format(float(105793)/2)                                                                                      
    '52896.5' 
Run Code Online (Sandbox Code Playgroud)

我正在尝试以十进制格式打印小数的某些数字,并以科学记数法打印大数的某些数字。

为什么 format 函数将某些浮点除法四舍五入为最接近的整数而不是其他?我在这里缺少什么?我已经尝试过 python2 和 3。

Tor*_* M. 6

“g”格式说明符默认四舍五入到 6 位有效数字。

https://python-reference.readthedocs.io/en/latest/docs/str/formatting.html

  • 您还可以在官方文档中找到此内容,[此处](https://docs.python.org/3/library/string.html#format-specification-mini-language)。 (2认同)