Tax rounding broke my invoice checks. What I learned building them

by•

I’m building an invoice extraction API, and part of it checks that the extracted amounts add up. My first version allowed a cent of difference everywhere. That was wrong in both directions: it flagged correct invoices and let some errors through.

Three things I learned:

  1. A check needs three answers, not two: it adds up, it doesn’t, or it can’t be decided. If a number is missing, don’t guess it to make the check pass.

  2. Tax needs rounding in any direction. Issuers round tax down, or take it out of a price that already includes it. A price of 691.34 including 10% tax carries 62.84 of tax once rounded down, leaving 628.50. But 628.50 × 10% is exactly 62.85, so a strict check fails a correct invoice by a cent.

  3. The arithmetic is the easy part. Every rule depends on a question the numbers can’t answer: does the subtotal already include tax? Do you have every line? You can’t decide that by checking which equation fits, or every check passes by construction.

I wrote up all the rules, with Python and TypeScript you can copy:

How do you validate extracted data? Fixed tolerances, or something else?

Add a comment

Replies

Be the first to reply

Have a question or a thought to share? Add a comment above to start the conversation.