ROUND_HALF_DOWN , Python 3.9.5, decimal 模块的行为
Behaviour of ROUND_HALF_DOWN , Python 3.9.5, decimal module
OS: fedora 34 工作站, GNU/Linux
我很困惑。在十进制模块中 documentation section "Rounding modes" 说,
decimal.ROUND_HALF_DOWN
Round to nearest with ties going towards zero.
我有一个例子
import decimal
from decimal import Decimal, getcontext
print('----')
getcontext().prec = 4
getcontext().rounding = decimal.ROUND_HALF_DOWN
print(getcontext())
n1 = Decimal('25.2525') + Decimal('0.003')
print(n1)
我期待的结果是 25.25,因为 n1 = 25.2555 的总和结果在千分之一的位置有一个 5,然后必须向下到 0 并且百分之一的位置保持不变。
我错了吗?
为什么我要到 25.26?
它说:
Round to nearest with ties going towards zero.
在这个特殊情况下,没有平局,因为 25.2555 比 25.25 更接近 25.26。所以它只是四舍五入到最近的值,即 25.26。
OS: fedora 34 工作站, GNU/Linux
我很困惑。在十进制模块中 documentation section "Rounding modes" 说,
decimal.ROUND_HALF_DOWN
Round to nearest with ties going towards zero.
我有一个例子
import decimal
from decimal import Decimal, getcontext
print('----')
getcontext().prec = 4
getcontext().rounding = decimal.ROUND_HALF_DOWN
print(getcontext())
n1 = Decimal('25.2525') + Decimal('0.003')
print(n1)
我期待的结果是 25.25,因为 n1 = 25.2555 的总和结果在千分之一的位置有一个 5,然后必须向下到 0 并且百分之一的位置保持不变。
我错了吗?
为什么我要到 25.26?
它说:
Round to nearest with ties going towards zero.
在这个特殊情况下,没有平局,因为 25.2555 比 25.25 更接近 25.26。所以它只是四舍五入到最近的值,即 25.26。