decimal
The Python decimal module provides support for correctly rounded decimal floating-point arithmetic. It offers several advantages over the built-in floating-point type, including preserving significant digits.
This module allows you to make precise calculations in finance and other fields that require accurate decimal representation.
Here’s a quick example:
>>> from decimal import Decimal
>>> Decimal("0.1") + Decimal("0.2")
Decimal('0.3')
>>> # Using the built-in float
>>> 0.1 + 0.2
0.30000000000000004
Try it with your own numbers to see what each type actually stores:
Most decimal fractions have no exact binary form, so float rounds to the nearest value it can hold, while Decimal keeps the digits you wrote.
Key Features
- Provides a
Decimaldata type for decimal floating-point arithmetic - Supports arbitrary precision arithmetic
- Offers configurable rounding modes
- Supports exact decimal representation of numbers
- Provides context objects for controlling precision and rounding
Frequently Used Classes and Functions
| Object | Type | Description |
|---|---|---|
decimal.Decimal |
Class | Creates decimal floating-point numbers |
decimal.getcontext() |
Function | Gets the current default context |
decimal.setcontext() |
Function | Sets the current default context |
decimal.Context |
Class | Encapsulates precision and rounding settings |
decimal.localcontext() |
Function | Returns a context manager that will set the current context |
Rounding modes:
| Mode | Rounds |
|---|---|
decimal.ROUND_CEILING |
Towards positive infinity |
decimal.ROUND_DOWN |
Towards zero |
decimal.ROUND_FLOOR |
Towards negative infinity |
decimal.ROUND_HALF_DOWN |
To nearest, with ties rounding toward zero |
decimal.ROUND_HALF_EVEN |
To nearest, with ties going to nearest even integer |
decimal.ROUND_HALF_UP |
To nearest, with ties rounding away from zero |
decimal.ROUND_UP |
Away from zero |
decimal.ROUND_05UP |
Away from zero if the last digit after rounding toward zero would have been 0 or 5. Otherwise, rounds toward zero |
Examples
Creating a Decimal object for precise arithmetic:
>>> from decimal import Decimal
>>> Decimal("1.1") + Decimal("2.2")
Decimal('3.3')
Setting precision for calculations:
>>> from decimal import Decimal, localcontext
>>> with localcontext(prec=42):
... Decimal("1") / Decimal("42")
...
Decimal('0.0238095238095238095238095238095238095238095')
>>> Decimal("1") / Decimal("42")
Decimal('0.02380952380952380952380952381')
Common Use Cases
- Performing precise financial calculations
- Handling user input that requires exact decimal representation
- Avoiding rounding errors in scientific computations
- Implementing configurable rounding strategies
Real-World Example
Suppose you need to calculate the total cost of items in a shopping cart with precise financial calculations:
>>> from decimal import Decimal
>>> prices = [Decimal("19.99"), Decimal("9.99"), Decimal("4.99")]
>>> total = sum(prices)
>>> total
Decimal('34.97')
This example demonstrates how the decimal module can be used to sum up monetary values accurately, avoiding the pitfalls of binary floating-point representation.
Related Resources
Tutorial
How to Round Numbers in Python
In this tutorial, you'll learn what kinds of mistakes you might make when rounding numbers and how you can best manage or avoid them. It's a great place to start for the early-intermediate Python developer interested in using Python for finance, data science, or scientific computing.
For additional information on related topics, take a look at the following resources:
- How to Format Floats Within F-Strings in Python (Tutorial)
- Numbers in Python (Tutorial)
- Rounding Numbers in Python (Course)
- Rounding Numbers in Python (Quiz)
- Formatting Floats Inside Python F-Strings (Course)
- Format Floats Within F-Strings (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Oct. 6, 2026