PyDocs

Nav

Numeric and Mathematical Modules

localcontext

decimal

function
  • localcontext(ctx=None, **kwargs)
Return a context manager for a copy of the supplied context.

That will set the default context to a copy of ctx on entry to the
with-statement and restore the previous default context when exiting
the with-statement. If no context is specified, a copy of the current
default context is used.
Changed in 3.11
  • 3.11: localcontext() now supports setting context attributes through the use of keyword arguments.
1 Example
>>> format(Decimal('2.675'), "f")
'2.675'
>>> format(Decimal('2.675'), ".2f")
'2.68'
>>> f"{Decimal('2.675'):.2f}"
'2.68'
>>> format(Decimal('2.675'), ".2e")
'2.68e+0'
>>> with localcontext() as ctx:
...     ctx.rounding = ROUND_DOWN
...     print(format(Decimal('2.675'), ".2f"))
...
2.67
from the Python docs
See Also
No see-alsos for decimal/localcontext
0 Notes
No notes for localcontext