Python Glossary Русский

all Python 3Numbers & mathterm

decimal.Context

decimal

A decimal arithmetic context: precision (prec), rounding mode, exponent limits and which conditions raise exceptions.

Syntax

decimal.Context(prec=None, rounding=None, Emin=None, Emax=None, capitals=None, clamp=None, flags=None, traps=None)

prec is the number of significant digits, not decimal places: with prec=5 the result 12345.678 becomes 12346. A Context(...) you create affects nothing until you pass it to setcontext() or localcontext() or call its methods directly (ctx.divide(1, 3)). Precision applies to operation results, not to constructing Decimal('1.23456789').

Examples

from decimal import Context, Decimal
ctx = Context(prec=5)
print(ctx.divide(1, 3))  # → 0.33333
print(Decimal(1) / 3)  # → 0.3333333333333333333333333333
from decimal import Context, Decimal
print(Context(prec=5).plus(Decimal('12345.678')))  # → 12346

See also

Open in the full glossarydocs.python.orgReport an inaccuracy