The decimal module in Python provides support for fast and correctly rounded decimal floating point arithmetic. It offers several advantages over the float data type, including more precision and a configurable rounding mode.
Table of Contents
- Introduction
- Creating Decimal Objects
- Arithmetic Operations
- Methods and Functions
quantizesqrtexplnlog10
- Decimal Context
- Precision
- Rounding
- Context Functions
- Examples
- Basic Arithmetic
- Working with Decimal Context
- Rounding and Precision
- Real-World Use Case
- Conclusion
- References
Introduction
The decimal module provides a Decimal data type for decimal floating point arithmetic. Compared to binary floating point (the float type), the Decimal type has more precision and can be configured to use different rounding modes.
Creating Decimal Objects
You can create Decimal objects from integers, strings, or tuples.
from decimal import Decimal
d1 = Decimal(10)
d2 = Decimal('3.14')
d3 = Decimal((0, (3, 1, 4), -2))
print(d1) # 10
print(d2) # 3.14
print(d3) # 3.14
Output:
10
3.14
3.14
Arithmetic Operations
Decimal objects support all the usual arithmetic operations.
from decimal import Decimal
a = Decimal('1.1')
b = Decimal('2.2')
print(a + b) # 3.3
print(a * b) # 2.42
print(a / b) # 0.5
print(a - b) # -1.1
Output:
3.3
2.42
0.5
-1.1
Methods and Functions
quantize
The quantize method rounds a Decimal to a fixed exponent.
from decimal import Decimal
d = Decimal('1.2345')
q = Decimal('0.01')
print(d.quantize(q)) # 1.23
Output:
1.23
sqrt
The sqrt method returns the square root of a Decimal.
from decimal import Decimal
d = Decimal('16')
print(d.sqrt()) # 4
Output:
4
exp
The exp method returns the exponential of a Decimal.
from decimal import Decimal
d = Decimal('1')
print(d.exp()) # 2.718281828459045
Output:
2.718281828459045235360287471
ln
The ln method returns the natural logarithm of a Decimal.
from decimal import Decimal
d = Decimal('10')
print(d.ln()) # 2.302585092994046
Output:
2.302585092994045684017991455
log10
The log10 method returns the base-10 logarithm of a Decimal.
from decimal import Decimal
d = Decimal('100')
print(d.log10()) # 2
Output:
2
Decimal Context
A Decimal context controls precision, rounding, and other settings. You can use the getcontext function to access and modify the current context.
Precision
You can set the precision for Decimal arithmetic.
from decimal import Decimal, getcontext
getcontext().prec = 5
d = Decimal('1.123456')
print(d) # 1.1235
Output:
1.123456
Rounding
You can set the rounding mode for Decimal arithmetic.
from decimal import Decimal, getcontext, ROUND_DOWN
getcontext().rounding = ROUND_DOWN
d = Decimal('1.2345')
print(d.quantize(Decimal('0.01'))) # 1.23
Output:
1.23
Context Functions
You can use the context for various operations.
from decimal import Decimal, getcontext
context = getcontext()
d = Decimal('123.456')
print(context.sqrt(d)) # 11.111
Output:
11.11107555549866648462149404
Examples
Basic Arithmetic
Perform basic arithmetic operations with Decimal objects.
from decimal import Decimal
a = Decimal('2.5')
b = Decimal('3.5')
print(a + b) # 6.0
print(a - b) # -1.0
print(a * b) # 8.75
print(a / b) # 0.7142857142857142857142857143
Output:
6.0
-1.0
8.75
0.7142857142857142857142857143
Working with Decimal Context
Set precision and rounding mode using the Decimal context.
from decimal import Decimal, getcontext, ROUND_HALF_UP
getcontext().prec = 4
getcontext().rounding = ROUND_HALF_UP
d = Decimal('1.23456')
print(d) # 1.235
Output:
1.23456
Rounding and Precision
Demonstrate different rounding modes and precision settings.
from decimal import Decimal, getcontext, ROUND_UP, ROUND_DOWN
d = Decimal('1.23456')
# ROUND_UP
getcontext().rounding = ROUND_UP
print(d.quantize(Decimal('0.01'))) # 1.24
# ROUND_DOWN
getcontext().rounding = ROUND_DOWN
print(d.quantize(Decimal('0.01'))) # 1.23
Output:
1.24
1.23
Real-World Use Case
Financial Calculations
In financial applications, precise decimal arithmetic is crucial to avoid rounding errors that can occur with binary floating-point arithmetic.
from decimal import Decimal
price = Decimal('19.99')
quantity = Decimal('3')
total = price * quantity
print(total) # 59.97
Output:
59.97
Conclusion
The decimal module in Python provides used for performing precise decimal arithmetic. It offers more control over precision and rounding compared to binary floating-point arithmetic, making it particularly useful for financial and scientific applications.