DEV Community

Tidy Cents
Tidy Cents

Posted on

Reading a reconciliation difference in integer cents: swaps, wrong sides and decimal slips

When a bank reconciliation does not balance, the difference is a number with structure. Three tests catch most single keying errors, and all three are exact in integer cents.

Work in cents

const toCents = s => {               // "1,198.75" -> 119875n
  const [w, f = ""] = s.replace(/,/g, "").split(".");
  return BigInt(w) * 100n + BigInt((f + "00").slice(0, 2));
};
const diff = 563345n - 491345n;      // 72000n  ($720.00)
const abs = diff < 0n ? -diff : diff;
Enter fullscreen mode Exit fullscreen mode

1. Transposition: divisible by 9

Swapping two adjacent digits changes a number by 9 * (a - b) * 10^k, so the difference is a multiple of 9 cents.

function swapCandidates(entries, abs) {
  if (abs % 9n !== 0n) return [];
  const out = [];
  for (const e of entries) {
    const s = e.toString();
    for (let i = 0; i < s.length - 1; i++) {
      const t = s.slice(0, i) + s[i + 1] + s[i] + s.slice(i + 2);
      const d = BigInt(t) - e;
      if (d === abs || d === -abs) out.push({ entry: e, keyedAs: BigInt(t) });
    }
  }
  return out;
}
// 119875n keyed as 191875n -> +72000n
Enter fullscreen mode Exit fullscreen mode

2. Wrong side: exactly twice an entry

A debit posted as a credit moves the balance by double the amount.

const wrongSide = entries.filter(e => 2n * e === abs);
Enter fullscreen mode Exit fullscreen mode

3. Decimal slip: nine times the smaller amount

4500n typed as 45000n moves the balance by 40500n = 9 * 4500n.

The recorded entry is either the smaller amount (450.00 typed as 45.00) or ten times it (45.00 typed as 450.00), so check both directions:

const decimal = entries.filter(e => 9n * e === abs || 9n * e === 10n * abs);
// 4500n recorded as 45000n: 9n * 45000n === 10n * 40500n
Enter fullscreen mode Exit fullscreen mode

Note that tests 1 and 3 both need divisibility by 9, so report every candidate with its arithmetic and let the person check the source document. Two overlapping errors can hide each other; say so instead of guessing.

We use these tests in the free Bank Reconciliation Difference Finder.

Top comments (0)