Back to results for “Design Patterns in JavaScript”
JavaScript › Numbers, Math & BigInt

Why is BigInt generally the wrong tool for representing monetary amounts with decimals, like $19.99?

Advanced

Answer

BigInt only represents whole integers — it has no concept of a decimal point. The common workaround is to store money as an integer count of the smallest currency unit (e.g. cents) rather than trying to represent fractional dollars directly, whether using BigInt or regular Numbers.
// Store cents as an integer instead of dollars as a float
const priceInCents = 1999; // represents $19.99
const total = priceInCents * quantity; // exact integer math, no float errors
Real-world example Avoiding floating-point rounding errors in an e-commerce checkout by doing all money math in integer cents.

Common follow-ups: What dedicated libraries exist for precise decimal arithmetic in JavaScript, like decimal.js?