Python3.4 将浮点数限制为两位小数

Python3.4 limiting floats to two decimal points

我正在使用 python 3.4 我想将浮点数限制为两位小数

round(1.2377, 2)
format(1.2377, '.2f')

这两个可以给我1.24,但我不想要1.24,我需要1.23,我该怎么做?

您可以转换为字符串和切片,然后再转换为浮点数:

>>> num=1.2377
>>> float(str(num)[:-2])
1.23

阅读更多关于 Floating Point Arithmetic: Issues and Limitations