01 / Reproduce
Compare both totals
Extract the practice ZIP to a fresh folder. Open PowerShell in that folder and run the script. The three jobs in usage.json are fictional and each reports 0.005 credit.
python .\compare-rounding.py
Raw total 0.015
Sum of rounded jobs 0.03
Rounded batch total 0.02
The row approach rounds each 0.005 to 0.01, then adds the three. The batch approach adds them to get 0.015, then rounds once to 0.02. In both paths, ties round away from zero.
02 / Inspect
Find the rounding step
The script reads JSON decimal fractions with parse_float=Decimal, so 0.005 goes directly from its JSON text into a decimal value. The explicit rounding argument gives both calculations the same tie rule. Decimal("0.01") sets the result to two decimal places.
from decimal import Decimal, ROUND_HALF_UP
step = Decimal("0.01")
credits = [Decimal("0.005")] * 3
rounded_jobs = sum(n.quantize(step, rounding=ROUND_HALF_UP) for n in credits)
rounded_batch = sum(credits).quantize(step, rounding=ROUND_HALF_UP)
print(rounded_jobs, rounded_batch)
The snippet prints 0.03 0.02. Each line calls the same rounding method. In the first line the method runs inside the sum. In the second it runs after the sum.
03 / Make it your own
Add one more job
Open usage.json, add a comma after the third object, and append the object below inside the square brackets. Run the same script again.
{"job": "J-104", "credits": 0.005}
The raw total is now 0.020. The sum of rounded rows rises to 0.04, while the rounded batch total stays 0.02. Save this fixture as a regression case if your report promises a batch total. Assert that the report shows 0.02 and label the per-job values as rounded for display.
Boundaries
Specify what your report totals
Write the rounding stage and tie rule into the report specification. If you use AI to draft the implementation, try this instruction.
Sum the raw decimal credits before rounding the batch total to two places. Use half-up rounding. Label the individual displayed values as rounded.
The script assumes a reviewed fixture with small, finite numeric credits that fit within the decimal context’s precision. Validate the shape and numeric bounds of any external input before using this pattern. The exercise verified local arithmetic on authored data. The suggested prompt, model behavior, provider billing, and performance were outside the exercise.
Primary sources / Checked November 15, 2026
Read the Python references
- How the JSON decoder passes decimal literals to a custom parser. Python JSON decoding
- How
quantizesets the exponent and applies the specified rounding mode. Decimal.quantize - How half-up handles ties. ROUND_HALF_UP
Downloads / Practice and social files

