June 2019
Beginner to intermediate
770 pages
19h 24m
English
Another context that is used frequently is the decimal context. This context defines a number of properties of the decimal.Decimal calculation, including the quantization rules used to round or truncate values.
We might see application programming that looks similar to the following code snippet:
import decimal
PENNY = decimal.Decimal("0.00")
price = decimal.Decimal('15.99')
rate = decimal.Decimal('0.0075')
print(f"Tax={(price * rate).quantize(PENNY)}, Fully={price * rate}")
with decimal.localcontext() as ctx:
ctx.rounding = decimal.ROUND_DOWN
tax = (price*rate).quantize(PENNY)
print(f"Tax={tax}")
The preceding example shows both a default context as well as a local context. The default context is shown first, and ...
Read now
Unlock full access