Decimal.getcontext.prec() 无法强制执行精度

Decimal.getcontext.prec() fails to enforce precision

In [1]: from decimal import Decimal, getcontext

In [2]: getcontext().prec = 4

In [3]: Decimal('0.12345')
Out[3]: Decimal('0.12345')

In [4]: Decimal('0.12345') * Decimal('0.12345')
Out[4]: Decimal('0.01524')

我期待第二次 '0.1234''0.0152'

有办法实现吗?

Decimal FAQ:

Q. I noticed that context precision is applied to the results of operations but not to the inputs. Is there anything to watch out for when mixing values of different precisions?

A. [...] The solution is either to increase precision or to force rounding of inputs using the unary plus operation.

>>> from decimal import Decimal, getcontext
>>> getcontext().prec = 4
>>> +Decimal('0.12345')
Decimal('0.1234')
>>> (+Decimal('0.12345') * +Decimal('0.12345'))
Decimal('0.01523')
>>>