First page Back Continue Last page Overview Graphics
Example of Using Context
>>> import decimal
>>> ctx = decimal.getcontext()
>>> ctx.prec = 3
>>> ctx.rounding = decimal.ROUND_HALF_UP
>>> x = decimal.Decimal("1.25")
>>> y = decimal.Decimal("0.666666")
>>> y
Decimal("0.666666")
>>> x * y
Decimal("0.833")
Notes:
...But I'll give one example of using context objects. Here we get the per-thread context object, and set the precision to just 2, and specify that the rounding is ROUND_HALF_UP.
Notice that the precision doesn't affect an object where we specify that it has 6 digits. But when we multiply x and y, the result gets rounded off to the current precision.