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

Python string format a float, how to truncate instead of rounding

是否有这样的格式选项

>>> '%.1(format)'%2.85

给出“2.8”?

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

>>> "{:.0f}".format(2.85)
'3'
>>> "%.0f"%2.85
'3'
>>> "%.1f"%2.85
'2.9'
>>> "%i"%2.85
'2'

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

你需要round in your code instead