Skip to main content

SARAVANA KUMAR · TECHNICAL KNOWLEDGE · Intermediate

A revenue reconciliation differs by a rounding residue

Use decimal input and an explicit rounding policy for currency transformations.

Illustrative learning scenario; not a claimed client engagement. A fictional CSV ingestion exercise receives decimal currency text. The source total is 0.30, but a binary floating-point comparison rejects the transformed total. The lab isolates representation error; it does not explain every financial mismatch.

Symptoms

Use decimal input and an explicit rounding policy for currency transformations.

Diagnosis

  1. Compare raw numeric strings with parsed values before formatting the result for display.
  2. Separate representation error from duplicates, missing records, currency conversion and genuine source adjustments.
  3. Ask whether rounding happens per line or after aggregation. Those policies can produce different totals and must be agreed explicitly.

Runnable minimal example

Python 3 standard library. Save as lab.py and run python3 lab.py; no network or persistent storage.

from decimal import Decimal, ROUND_HALF_UP

amounts = ['0.10', '0.20']
print(sum(float(x) for x in amounts) == 0.30)
total = sum((Decimal(x) for x in amounts), Decimal('0'))
print(total == Decimal('0.30'))
print(total.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP))

Expected output — verified locally

False
True
0.30

Fix and boundaries

Parse the original decimal text directly into Decimal and use an agreed currency scale and rounding mode at the defined boundary. In a warehouse, carry that contract into fixed-precision decimal columns. Do not construct Decimal from an already rounded binary float and expect to recover the original text.

Why it works

Values such as 0.10 cannot be represented exactly in binary floating point. Decimal constructs the fixture values from their base-ten strings, so their sum compares exactly with 0.30. Quantize makes the output scale explicit; ROUND_HALF_UP is a lab policy rather than a universal financial rule.

Prevention

Validate numeric syntax, finiteness, precision and scale at ingestion. Test negative adjustments and half-cent boundaries. Reconcile by currency and source batch; never add unlike currencies into one total.

Practice exercise

Use two lines of 0.005. Compare rounding each line to cents before summing with rounding once after summing. Explain the 0.02 versus 0.01 difference and document which rule your hypothetical contract uses.

Documentation