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
Key Features
- Provides a
Decimal
data 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 postive 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 digit dropped is 0 or 5. Otherwise, rounds towards 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
Numbers in Python
In this tutorial, you'll learn about numbers and basic math in Python. You'll explore integer, floating-point numbers, and complex numbers and see how perform calculations using Python's arithmetic operators, math functions, and number methods.
For additional information on related topics, take a look at the following resources:
- How to Round Numbers in Python (Tutorial)
- Rounding Numbers in Python (Course)
- Rounding Numbers in Python (Quiz)
By Leodanis Pozo Ramos • Updated June 26, 2025