以下示例展示对于结构相同的两组数据(1.03575000
和1.03425000
)保留小数后4位,使用不同方法的输出结果
from decimal import Decimal, ROUND_HALF_UP print(round(1.03575000, 4)) print(round(1.03425000, 4)) print('--------------------') print(round(Decimal("1.03575000"), 4)) print(round(Decimal('1.03425000'), 4)) print('--------------------') print(Decimal("1.03575000").quantize(Decimal("0.0000"), rounding=ROUND_HALF_UP)) print(Decimal("1.03425000").quantize(Decimal("0.0000"), rounding=ROUND_HALF_UP)) """ 1.0357 1.0342 -------------------- 1.0358 1.0342 -------------------- 1.0358 1.0343 """