Files
Learn-Python-Programming-Th…/ch07/context/decimal.prec.py
T
adii1823 32c12d89f4 ch07
2021-10-28 17:39:13 +05:30

47 lines
914 B
Python

# context/decimal.prec.py
from decimal import Context, Decimal, getcontext, setcontext
one = Decimal("1")
three = Decimal("3")
orig_ctx = getcontext()
ctx = Context(prec=5)
setcontext(ctx)
print(ctx)
print(one / three)
setcontext(orig_ctx)
print(one / three)
"""
$ python context/decimal.prec.py
Context(prec=5, rounding=ROUND_HALF_EVEN, Emin=-999999,
Emax=999999, capitals=1, clamp=0, flags=[],
traps=[InvalidOperation, DivisionByZero, Overflow])
0.33333
0.3333333333333333333333333333
"""
orig_ctx = getcontext()
ctx = Context(prec=5)
setcontext(ctx)
try:
print(ctx)
print(one / three)
finally:
setcontext(orig_ctx)
print(one / three)
from decimal import localcontext
with localcontext(Context(prec=5)) as ctx:
print(ctx)
print(one / three)
print(one / three)
with localcontext(Context(prec=5)), open("out.txt", "w") as out_f:
out_f.write(f"{one} / {three} = {one / three}\n")