Python decimal - fixed-precision Decimal arithmetic and rounding contexts
Answer: Decimal numbers can be represented exactly, which offers several advantages over the float datatype. Use Decimal values with a context that sets precision and rounding.
Examples
from decimal import Decimal, getcontext, localcontext, ROUND_HALF_UP
getcontext().prec = 6
print(Decimal("1") / Decimal("7")) # 0.142857
price = Decimal("19.99")
fee = (price * Decimal("0.075")).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
with localcontext() as ctx:
ctx.prec = 50
print(+Decimal("1") / Decimal("7")) # unary + applies the context
Rules
- Prefer a string argument such as
Decimal("1.1")so the value can be represented exactly. getcontext()is the current context.localcontext()temporarily changes it.quantize()rounds a Decimal to a fixed exponent.
See also
Claims
- id: C1 claim: Decimal can represent numbers exactly. source: S1 evidence: "Decimal numbers can be represented exactly"
- id: C2 claim: Decimal has advantages over float. source: S1 evidence: "It offers several advantages over the float datatype"
- id: C3 claim: The module implements decimal arithmetic. source: S2 evidence: "decimal arithmetic module"
Sources
- [S1] decimal - Decimal fixed-point and floating-point arithmetic. Python docs. https://docs.python.org/3/library/decimal.html . License: PSF-2.0. Accessed 2026-11-18.
- [S2] Lib/_pydecimal.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/_pydecimal.py . License: PSF-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, decimal - Decimal fixed-point and floating-point arithmetic; python/cpython (GitHub), Lib/_pydecimal.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, decimal - Decimal fixed-point and floating-point arithmetic; python/cpython (GitHub), Lib/_pydecimal.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python decimal - fixed-precision Decimal arithmetic and rounding contexts." iinventedlight Library, iil:note:kvndq5q4, version of 2026-11-18. https://iinventedlight.com/library/notes/python-decimal/. License: CC-BY-4.0.
@misc{iil_note_kvndq5q4,
author = {{iinventedlight}},
title = {{Python decimal - fixed-precision Decimal arithmetic and rounding contexts}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/python-decimal/},
note = {Permanent ID iil:note:kvndq5q4. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-decimal.md.