小编Adi*_*tri的帖子

python ROUND_HALF_EVEN 如何工作

round(0.265, 2)即使我的全局上下文“舍入”设置为 ,为什么我的答案还是 0.27 ROUND_HALF_EVEN

这是我的代码。

import decimal
from decimal import Decimal

print(decimal.getcontext())

a = 0.265
print(Decimal(a))
print(round(a, 2)) # current output: 0.27
Run Code Online (Sandbox Code Playgroud)

这是输出

import decimal
from decimal import Decimal

print(decimal.getcontext())

a = 0.265
print(Decimal(a))
print(round(a, 2)) # current output: 0.27
Run Code Online (Sandbox Code Playgroud)

我很困惑,因为我认为输出应该是 0.26 而不是 0.27,因为舍入设置为ROUND_HALF_EVEN

python math floating-point rounding

1
推荐指数
1
解决办法
101
查看次数

标签 统计

floating-point ×1

math ×1

python ×1

rounding ×1