Tax rounding broke my invoice checks. What I learned building them
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:
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.
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.
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: https://temlavo.com/blog/validate-invoice-totals
How do you validate extracted data? Fixed tolerances, or something else?
p/qapilot
p/runevr
p/google-cloud-platform
p/producthunt
p/tag-your-photos
p/dockduck
p/visitors
p/smorza
p/vitals-3
p/mindpal-ai
p/teaserkit
p/dex-by-exmergo
p/fetchsandbox
p/reso
p/raycast
p/tulu
p/dots-7
p/many-displays
p/lingodotdev
Replies
Be the first to reply
Have a question or a thought to share? Add a comment above to start the conversation.