decimal — Easy Examples
Arbitrary-precision decimal floating-point arithmetic
Getting started with decimal
Basic import and usage of the decimal module.
python
from decimal import Decimal, getcontext # Precise decimal arithmetic print(0.1 + 0.2) # Float imprecision print(Decimal("0.1") + Decimal("0.2")) # Exact getcontext().prec = 50 pi = Decimal("3.14159265358979323846264338327950288419716939937510") print(f"Pi to 50 digits: {pi}")
The decimal module is part of Python's standard library. Arbitrary-precision decimal floating-point arithmetic.
Common decimal operations
Frequently used functions from the decimal module.
python
# More decimal examples import decimal print(f"decimal module loaded successfully") print(f"Location: {decimal.__name__}") print(f"Has {len(dir(decimal))} attributes")
These are the most commonly used features of the decimal module.
Want to try these examples interactively?
Open Easy Playground