Control decimal precision and rounding with decimal.Context and localcontext.
What It Does and Why It Matters
The decimal context controls precision, rounding mode, exponent limits, and exceptional conditions for arithmetic. localcontext() temporarily adjusts settings so changes do not unexpectedly affect unrelated calculations.
Context precision controls significant digits during arithmetic; quantize() is often used for fixed decimal places.
Step-by-Step Example
Start with the complete example below. Read the inputs and predict what the final print statement will show before you run it.
from decimal import Decimal, localcontext, ROUND_HALF_UP
with localcontext() as decimal_context:
decimal_context.prec = 5
decimal_context.rounding = ROUND_HALF_UP
print(Decimal('1') / Decimal('7'))
print(Decimal('1.005').quantize(Decimal('0.01'), rounding=ROUND_HALF_UP))
How the Code Works
- The imports make these standard-library tools available: decimal.
- The structured block controls error handling or resource lifetime, including what happens when an operation fails.
- The print calls display the results. Compare these results with the method’s documented return type and the input used.
- The example chooses the numeric representation deliberately. Compare exact decimal or rational operations with float before changing values or rounding the result.
Expected output:
0.14286
1.01
Each displayed line comes from the corresponding calculation or check. The output helps verify the return values before you adapt the example to your own input.
What to Notice
Pay attention to the accepted input types, the returned value and the boundary behavior. Predict what changes if you replace the example input with an empty, zero, negative or missing value, where that value is meaningful for this operation.
If the task uses a library function, inspect its documented exceptions and return type before reusing it inside a larger function.
Where You Would Use This
Use the technique when it makes a real task easier to understand or safer to maintain. Check input and output behavior with small tests before combining it with larger application code.
Practice Exercise
Compare ROUND_HALF_EVEN and ROUND_HALF_UP on values ending in five.
Before running your edited example, write down the result you expect. Then test at least one valid boundary or deliberately invalid value, using appropriate exception handling where necessary.
Reference: official Python documentation for the complete API contract and version notes.
Common Beginner Mistakes
- Changing the global decimal context for a local calculation and surprising other code.
- Overlooking the documented condition: the decimal context controls precision, rounding mode, exponent limits, and exceptional conditions for arithmetic. localcontext() temporarily adjusts settings so changes do not unexpectedly affect unrelated calculations.
Key Takeaways
- Control decimal precision and rounding with decimal.Context and localcontext.
- The decimal context controls precision, rounding mode, exponent limits, and exceptional conditions for arithmetic.
- Compare ROUND_HALF_EVEN and ROUND_HALF_UP on values ending in five.