Use decimal.Decimal when financial rules require decimal arithmetic.
What It Does and Why It Matters
Binary floating-point cannot represent most decimal fractions exactly, so repeated money calculations can accumulate tiny errors. Decimal accepts decimal strings exactly and lets you specify rounding explicitly. Create values from strings, not from floats, when the original amounts are decimal. A monetary application also needs a clear currency, precision and rounding policy.
Decimal improves decimal precision, but does not determine the legally appropriate rounding rule.
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, ROUND_HALF_UP
unit_price = Decimal("12.35")
quantity = 3
subtotal = unit_price * quantity
tax = (subtotal * Decimal("0.17")).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
print(subtotal)
print(tax)
print(subtotal + tax)
How the Code Works
- The imports make these standard-library tools available: decimal.
- The name unit_price receives the value of Decimal('12.35'). Changing this input lets you explore a different case.
- The print calls display the results. Compare these results with the method’s documented return type and the input used.
- Each amount starts as a decimal string. The subtotal is computed first; tax is rounded explicitly to cents before the printed grand total is formed.
Expected output:
37.05
6.30
43.35
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
Change quantity and tax rate; decide whether tax is rounded per item or for the whole order.
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
- Writing Decimal(0.1) and expecting it to equal Decimal("0.1"), or rounding only after mixing currencies.
- Overlooking the documented condition: binary floating-point cannot represent most decimal fractions exactly, so repeated money calculations can accumulate tiny errors.
Key Takeaways
- Use decimal.Decimal when financial rules require decimal arithmetic.
- Binary floating-point cannot represent most decimal fractions exactly, so repeated money calculations can accumulate tiny errors.
- Change quantity and tax rate; decide whether tax is rounded per item or for the whole order.