Rounding the total gives 0.03 in the synthetic call-cost fixture. In the synthetic call-cost fixture, rounding each cost with ROUND_HALF_UP before summing gives 0.04. To choose a calculation for your ledger, identify whether its posting rule sets the payable amount at the call or rollup level.
The problem
A ledger can go wrong if it treats the “round each” sum as interchangeable with the “round total” sum. The fixture isolates the rounding stage by feeding the same costs, quantum, and rounding mode into both calculations.
Reproduce the result
The fixture starts with values 5 and 25 and constructs costs by dividing each Decimal value by Decimal(1000). The each calculation quantizes every cost with cent and ROUND_HALF_UP before summing. The total calculation sums the costs before quantizing with the same arguments. The code contains an assertion that each minus total equals cent.
The following example ran on Python 3.11.9 with synthetic data.
values = [5, 25] from decimal import Decimal, ROUND_HALF_UP costs = [Decimal(value) / Decimal(1000) for value in values] cent = Decimal("0.01") each = sum(cost.quantize(cent, rounding=ROUND_HALF_UP) for cost in costs) total = sum(costs).quantize(cent, rounding=ROUND_HALF_UP) assert each - total == cent print("round each:", each) print("round total:", total) print("ROUND_HALF_UP from synthetic costs:", ", ".join(str(cost) for cost in costs)) print("unrounded sum:", sum(costs)) round each: 0.04 round total: 0.03 ROUND_HALF_UP from synthetic costs: 0.005, 0.025 unrounded sum: 0.030 What the output says
The line “ROUND_HALF_UP from synthetic costs: 0.005, 0.025” reports the shared inputs. The line “unrounded sum: 0.030” reports their sum before quantization. The line “round each: 0.04” reports the sum after per-call quantization. The line “round total: 0.03” reports the result of quantizing the sum.
Decimal.quantize uses the supplied rounding argument when rounding is needed. The per-call code passes ROUND_HALF_UP as the rounding argument to quantize. The unrounded sum already lies on a cent boundary, so quantizing it preserves its numeric value.
Limits
The fixture exercises positive synthetic costs with Decimal("0.01") as the quantum and an explicit ROUND_HALF_UP argument. The code supplies a rounding mode to quantize and uses ordinary Decimal arithmetic for division and summation. Check the provider’s published invoice policy before applying the fixture to billing.
Apply the check
For a ledger that uses ROUND_HALF_UP and posts calls at cent precision, compare its result with the per-call fixture. For a ledger that uses ROUND_HALF_UP and keeps fractional-cent costs until rollup, compare against the total fixture. Check the provider's invoice rounding policy before using either calculation for reconciliation.
Keep the positive-cost fixture as a regression test for the rounding stage. For a ledger that supports credits, add a separate fixture that replaces the printed 0.005 cost with its negative and retains 0.025. Check the credit fixture against the ledger's posting policy.
Primary reference: Python documentation
AI disclosure: This article was generated by an automated Codex editorial workflow.


