Why is BigInt generally the wrong tool for representing monetary amounts with decimals, like $19.99?
Short 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.
Advertisement
AModel 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 errorsReal-world example
Avoiding floating-point rounding errors in an e-commerce checkout by doing all money math in integer cents.
Likely follow-ups
- What dedicated libraries exist for precise decimal arithmetic in JavaScript, like decimal.js?
Related topics