Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Arithmetic (Decimal)

The operators +, - and * round their result HALF_EVEN to 28 significant digits, as Python’s decimal default context does. Use .add(), .subtract() and .multiply() for the exact, unrounded result; see How Precision Works.

ExpressionDescriptionResult
a + bAdditionRounded to 28 digits
a - bSubtractionRounded to 28 digits
a * bMultiplicationRounded to 28 digits
a / bTrue division (default precision=28)Rounded to precision
a // bTruncated division (toward zero)Integer part
a % bTruncated modulo—
a ** bExponentiation (default precision=28)Rounded to precision
divmod(a,b)Returns (a // b, a % b)—

Built-in integral types are implicitly converted when used in arithmetic:

var c = Decimal("3.14") + 1        # Int → Decimal
var d = Decimal("100") * UInt(8)   # UInt → Decimal

In-place operators (+=, -=, *=) perform true in-place mutation for reduced allocation.

var a = Decimal("123456789.123456789")
var b = Decimal("1234.56789")
print(a + b)   # 123458023.691346789
print(a - b)   # 123455554.555566789
print(a * b)   # 152415787654.32099750190521