Python字符串格式化一个浮点数,如何截断而不是四舍五入

jf3*_*328 8 python string.format

是否有格式选项,以便

>>> '%.1(format)'%2.85
Run Code Online (Sandbox Code Playgroud)

给出'2.8'?

在 Python 2.7 中,'f' 格式四舍五入到最接近的

>>> "{:.0f}".format(2.85)
'3'
>>> "%.0f"%2.85
'3'
>>> "%.1f"%2.85
'2.9'
>>> "%i"%2.85
'2'
Run Code Online (Sandbox Code Playgroud)

Eri*_*ric 2

不,那里没有。查看文档以获取受支持的浮点格式说明符的完整列表。

您需要对代码进行四舍五入