@
davidli @
hahastudio @
pc10201 刚才看了一下decimal的定义,实际情况如下:
from decimal import *
b = Decimal('238.345')
getcontext()
返回的值为Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999, capitals=1, flags=[], traps=[DivisionByZero, Overflow, InvalidOperation])
其中rounding为ROUND_HALF_EVEN.
而ROUND_HALF_EVEN的描述为:Behave as for ROUND_HALF_UP if the digit to the left of the discarded fraction is odd; behave as for ROUND_HALF_DOWN if it's even. (Rounds towards the "nearest neighbor" unless both neighbors are equidistant, in which case, rounds towards the even neighbor.)
简单说就是如果为238.345 则会使用ROUND_HALF_DOWN,如果是238.355 则会使用ROUND_HALF_UP。
测试代码如下:
'{:.2f}'.format(Decimal('238.345')) //返回的是 238.34
'{:.2f}'.format(Decimal('238.355')) //返回的是 238.36
如有错误,请勘正~~