Python字符串格式化浮点,如何截断而不是舍入

2024-04-25 13:17:14 发布

您现在位置:Python中文网/ 问答频道 /正文

是否有这样的格式选项

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

给出“2.8”?

在Python2.7中,“f”格式舍入到最近的

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

Tags: format格式选项