Why can the same decimal tie produce two posted amounts?
quantize rounds to a target exponent. The rounding mode resolves values exactly halfway between candidates.
Python example and interpretation
At two decimal places, 2.345 becomes 2.34 with half-even and 2.35 with half-up. Agree on both the rule and whether it is applied per item or after aggregation.
from decimal import Decimal as D,ROUND_HALF_EVEN,ROUND_HALF_UP
[str(D('2.345').quantize(D('.01'),rounding=r)) for r in (ROUND_HALF_EVEN,ROUND_HALF_UP)]Result ['2.34', '2.35']
中文
同一个小数在不同半数舍入规则下为何得到两种账面值?
quantize 将值舍入到目标指数,舍入模式决定恰在两档中间时如何选择。
Python 算例与适用条件
2.345 保留两位,半偶取 2.34,半进取 2.35。对账双方要约定舍入规则以及在单笔还是汇总后舍入。
from decimal import Decimal as D,ROUND_HALF_EVEN,ROUND_HALF_UP
[str(D('2.345').quantize(D('.01'),rounding=r)) for r in (ROUND_HALF_EVEN,ROUND_HALF_UP)]结果 ['2.34', '2.35']