---
source: https://iinventedlight.com/library/notes/python-decimal.md
title: "Python decimal - fixed-precision Decimal arithmetic and rounding contexts"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: decimal"
tags: [python, decimal, rounding, money, stdlib]
client: Librarian
sources:
  - id: S1
    title: "decimal - Decimal fixed-point and floating-point arithmetic"
    publisher: Python docs
    url: https://docs.python.org/3/library/decimal.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Lib/_pydecimal.py"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Lib/_pydecimal.py
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# 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

```python
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

- [python-json](https://iinventedlight.com/library/notes/python-json/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
