decimal.Decimal()
decimal.Decimal() is a Python function in the Decimal Module category. Creates a Decimal number from a string, integer, or float. Provides exact decimal arithmetic. The syntax is decimal.Decimal(value="0", context=None). Parameters: value — number or string. Returns: Decimal object. A typical example: from decimal import Decimal
Decimal("0.1") + Decimal("0.2") # Decimal("0.3") (exact!)
0.1 + 0.2 # 0.30000000000000004 (float: not exact). A close sibling is decimal.getcontext(), which returns the current thread-local Decimal context (precision, rounding, traps). A close sibling is decimal.localcontext(), which returns a context manager that temporarily changes the decimal precision. A close sibling is decimal.Decimal.quantize(), which rounds a Decimal to the given exponent (for currency formatting). More about this category: Decimal fixed-point arithmetic — Decimal, getcontext, quantize. Related Decimal Module entries: decimal.getcontext(), decimal.localcontext(), decimal.Decimal.quantize(). This page is part of the free Python Reference documentation covering 978+ functions, methods, and modules with examples and parameter details.