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.
| Expression | Description | Result |
|---|---|---|
a + b | Addition | Rounded to 28 digits |
a - b | Subtraction | Rounded to 28 digits |
a * b | Multiplication | Rounded to 28 digits |
a / b | True division (default precision=28) | Rounded to precision |
a // b | Truncated division (toward zero) | Integer part |
a % b | Truncated modulo | — |
a ** b | Exponentiation (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