Lesson 49 of 296
Number limits: floating point, BigInt and money
What you will learn
- How JavaScript stores numbers
- Why decimal maths is not exact
- The safe integer limit
- How to compare decimals
- BigInt in detail
- How to handle money correctly
- Formatting numbers for users
On this page
How numbers are stored
All JavaScript numbers use the 64-bit floating point format, a standard called IEEE 754. "Floating point" means the decimal point can move, so the same format stores both very large and very small numbers. The trade-off: it has limited precision.
Why 0.1 + 0.2 is not 0.3
Computers store numbers in binary (base 2). Some decimals cannot be written exactly in binary, just like 1/3 cannot be written exactly in decimal (0.3333...). So 0.1 is stored as a value very slightly different from 0.1.
0.1 + 0.2 0.30000000000000004
0.1 + 0.2 === 0.3 falseThis is not a JavaScript bug. Python, Java and C# give the same result with the same type.
Comparing decimals
Check whether the difference is tiny:
Math.abs((0.1 + 0.2) - 0.3) < Number.EPSILON trueNumber.EPSILON is the smallest difference between 1 and the next number JavaScript can store (about 2.2 x 10 to the power -16). For bigger numbers, use a tolerance that fits your data, such as 0.0001.
Rounding traps
(1.005).toFixed(2) "1.00" (you might expect "1.01")1.005 is really stored as 1.00499999... so it rounds down. Do not trust toFixed for money calculations.
The safe integer limit
Whole numbers are only exact up to Number.MAX_SAFE_INTEGER:
Number.MAX_SAFE_INTEGER 9007199254740991 (2 to the power 53, minus 1)
9007199254740992 === 9007199254740993 true (both stored the same)
Number.isSafeInteger(9007199254740993) falseAbove this limit, numbers lose precision silently.
Real-world impact: some systems use very large numeric IDs (for example 64-bit database IDs or IDs from some social media APIs). If they arrive as JSON numbers, JavaScript can change the last digits. The fix: send them as strings.
BigInt
BigInt stores whole numbers of any size exactly.
const big = 9007199254740993n;
const also = BigInt("9007199254740993");
big + 1n 9007199254740994n
2n ** 100n a 31-digit exact numberRules:
- You cannot mix BigInt and normal numbers:
1n + 1throws aTypeError. Convert first:1n + BigInt(1). - Division drops the decimal part:
7n / 2nis3n. Mathfunctions do not work with BigInt.JSON.stringifycannot handle BigInt by default; it throws an error.typeof 1nis"bigint".
Use BigInt for very large IDs and exact large integer maths. Do not use it for everyday numbers.
Handling money correctly
Store money as whole numbers of the smallest unit: pence, cents.
const pricePence = 1999; £19.99
const quantity = 3;
const totalPence = pricePence * quantity; 5997, exactRules for money:
- Store and calculate in integer pence.
- Round only where the business rule says (for example, once for VAT on the total, or per line), using
Math.round. - Convert to pounds only for display.
- In databases, use an integer column or an exact
DECIMALtype, never a floating point column. - Store the currency next to the amount (GBP, EUR).
const subtotal = 5997;
const vat = Math.round(subtotal * 0.2); 1199
const total = subtotal + vat; 7196Formatting numbers for users
Intl.NumberFormat formats numbers and currency correctly for each country:
const gbp = new Intl.NumberFormat("en-GB", { style: "currency", currency: "GBP" });
gbp.format(7196 / 100) "£71.96"
new Intl.NumberFormat("en-GB").format(1234567.891) "1,234,567.891"
new Intl.NumberFormat("de-DE").format(1234567.891) "1.234.567,891"Different countries use different separators. Never build currency strings by hand.
Common mistakes
- Storing money as decimal pounds.
- Comparing decimals with
===. - Using
toFixedinside calculations. - Receiving big IDs as JSON numbers.
- Mixing BigInt and Number.
How to say it in an interview
"JavaScript numbers are IEEE 754 double-precision floats, so some decimals like 0.1 cannot be represented exactly and 0.1 plus 0.2 is not 0.3. I compare decimals using a tolerance such as Number.EPSILON. Integers are only exact up to Number.MAX_SAFE_INTEGER, about 9 quadrillion; beyond that I use BigInt, or strings for IDs. For money I store integer pence, round only at defined points, keep the currency code, and format with Intl.NumberFormat."
Key words
| Word | Meaning |
|---|---|
| Floating point | The number format that stores decimals with limited precision |
| IEEE 754 | The standard that defines that format |
Number.EPSILON | A tiny value used as a tolerance when comparing decimals |
Number.MAX_SAFE_INTEGER | The largest exact whole number: 9007199254740991 |
| BigInt | A type for exact whole numbers of any size |
| Minor units | The smallest unit of a currency, such as pence |
Intl.NumberFormat | A built-in tool to format numbers and currency |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.