Precision matters: Handling currency in JavaScript
Floating point math (IEEE 754) is dangerous for financial apps. A guide to BigInt and safe currency math patterns.
Axiom Labs
Field notes from the studio
Why JavaScript numbers break money
JS Number is a 64-bit float. It cannot exactly represent values like 0.1, so 0.1 + 0.2 becomes 0.30000000000000004.
In a tax calculator, that tiny error turns into off-by-one-kobo bugs. What is a meme in a todo app is a ticket in production.
Store money as integers
Represent currency as the smallest unit: kobo for NGN, cents for USD. NGN 1,234.56 becomes 123456 internally.
Convert to human-readable decimals only at the edges: UI, PDFs, or payment providers. Core business logic stays integer.
BigInt vs decimal helpers
BigInt plus a small helper library keeps arithmetic exact and dependency-free across Workers, Node, and browsers.
Decimal libraries (decimal.js, Dinero.js) add APIs for allocation and formatting at the cost of bundle size. Pick one approach and enforce it consistently.
Database DECIMAL columns are fine, but never parse them into floats in JS; keep them as strings or integers before doing math.
Practical patterns we ship
Arithmetic helpers that only accept bigint: add, subtract, and apply rates using basis points so percentages stay integer-friendly.
Explicit rounding rules: tax bands round down, gross-to-net rounds to nearest kobo, display always keeps two decimals but leaves the underlying BigInt intact.
User input never becomes a float. Parse strings, validate format, convert to BigInt, and reject malformed entries up front.
The payoff
Integer smallest units, BigInt logic, explicit rounding, and string-based formatting remove an entire class of money drift bugs.
If your app touches salaries, taxes, invoices, or subscriptions, treat "0.1 + 0.2" as a smell and fix the pattern once.
If this is your problem
A practical next step
If this article sounds like the thing you are trying to fix, these are the closest Axiom Labs services to start from.
Key takeaway
Precision beats convenience. Standardize on integer money, keep rounding rules explicit, and your financial surfaces stop drifting.
Useful? Send it to someone scoping a similar build.
Related Signals
Stay in the loopHow to scope an internal tools project without building the wrong thing
Internal tools go wrong when teams start from screens instead of workflow boundaries. Here is how to scope the project before the build drifts.
What to prepare before building a business system
Business systems projects go vague when the workflow is still trapped in tribal knowledge. Here is what to prepare before the build starts.
How to choose between a dashboard, internal tool, and full web app
Not every software problem needs the same product shape. Here is how to tell whether the right answer is a dashboard, an internal tool, or a full web app.
Follow the Studio
More field notes and build logs.
