Numbers: one type and floating point
A number in JavaScript is one 64-bit floating point value, and that one type covers every number the language has. There is no integer type and no separate float type: 1, -4, 0.5 and 1e21 are all numbers, and typeof answers "number" for each of them. The layout is the one the IEEE 754 standard defines, which is the same layout a C double uses.
The type is exact for the integers people count with, and it is not exact for most fractions, which is the source of the surprising results in this lesson. The first section shows the type itself, the second the sum that everyone meets once, the third the tolerance that replaces an equality test, and the fourth the function that turns a number into text for a report.
One number type
An integer and a fraction are the same type, so the literal 42 and the literal 42.0 produce the same value. Nothing in the language marks a number as an integer, and a division that comes out even returns a number that prints without a decimal point. Number.isInteger is the check that asks whether the value has a fraction, not whether the type is an integer type.
console.log('typeof 42:', typeof 42);
console.log('typeof 4.5:', typeof 4.5);
console.log('42 and 42.0 are equal:', 42 === 42.0);
console.log('integer:', Number.isInteger(42));
console.log('integer:', Number.isInteger(42.0));
console.log('integer:', Number.isInteger(42.5));
console.log('10 / 4:', 10 / 4);
console.log('10 / 5:', 10 / 5);
console.log('0.5 + 0.5:', 0.5 + 0.5);
node one-type.js
typeof 42: number
typeof 4.5: number
42 and 42.0 are equal: true
integer: true
integer: true
integer: false
10 / 4: 2.5
10 / 5: 2
0.5 + 0.5: 1
The two divisions are the point of the section. 10 / 4 keeps its fraction, and 10 / 5 prints as 2 rather than 2.0, because the printed form drops a fraction that is zero. There is no division that rounds, and no operator that produces an integer from two integers: the language always divides as a float, and the rounding functions in the lesson on Math are how a program asks for an integer result.
0.1 + 0.2
A binary fraction can store a half, a quarter and an eighth exactly, and it cannot store one tenth: the value is a repeating fraction in base two, and the stored number is the nearest 64-bit value to it. The error of each literal is far below anything a person notices, and the sum of the two errors is visible when the result is printed with enough digits. The program below prints the sum, the comparison that fails and the exact stored value.
const sum = 0.1 + 0.2;
console.log('sum:', sum);
console.log('equal to 0.3:', sum === 0.3);
console.log('0.3 - sum:', 0.3 - sum);
console.log('toFixed(2):', sum.toFixed(2));
console.log('toFixed(20):', sum.toFixed(20));
node precision.js
sum: 0.30000000000000004
equal to 0.3: false
0.3 - sum: -5.551115123125783e-17
toFixed(2): 0.30
toFixed(20): 0.30000000000000004441
The last line is the same value with twenty decimals, and it is the clearest answer to what the machine holds: 0.30000000000000004441 is the nearest 64-bit float to three tenths after the addition. The comparison failed for that reason and not because === is broken, so a program that compares money or measurements with === needs either integers or a tolerance.
Number.EPSILON and a tolerance
Number.EPSILON is the difference between one and the next value above one, which is the smallest gap the type has at that size. It is not a universal accuracy: a number around a million has a gap about a billion times larger, so a useful comparison takes the size of the operands into account. The function below multiplies the epsilon by the largest magnitude it sees and accepts a difference within that.
console.log('EPSILON:', Number.EPSILON);
console.log('gap of 0.1 + 0.2:', Math.abs(0.1 + 0.2 - 0.3));
console.log('gap is below EPSILON:', Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON);
function nearlyEqual(a, b) {
const scale = Math.max(1, Math.abs(a), Math.abs(b));
return Math.abs(a - b) <= Number.EPSILON * scale;
}
console.log('0.1 + 0.2 near 0.3:', nearlyEqual(0.1 + 0.2, 0.3));
console.log('1 near 1.0001:', nearlyEqual(1, 1.0001));
console.log('1e16 near 1e16 + 2:', nearlyEqual(1e16, 1e16 + 2));
console.log('1e16 near 1e16 + 4:', nearlyEqual(1e16, 1e16 + 4));
node epsilon.js
EPSILON: 2.220446049250313e-16
gap of 0.1 + 0.2: 5.551115123125783e-17
gap is below EPSILON: true
0.1 + 0.2 near 0.3: true
1 near 1.0001: false
1e16 near 1e16 + 2: true
1e16 near 1e16 + 4: false
The last two lines of the output are the reason for the function: the gap around 1e16 is 2, so a tolerance that is fixed at one epsilon would call two neighbouring values different. The function accepts them and refuses a difference twice as large, which is the behaviour a test wants. A tolerance is a decision about the data, and it belongs in the code that knows what the numbers mean.
Text with a fixed number of decimals
toFixed returns a string with the requested number of decimals, which is what goes on an invoice or into a table. It rounds the stored value, not the decimal that the source code looks like, so a value written as 1.005 can round down: the literal is slightly below one and five thousandths. The result of toFixed is text, so a program that needs a number again passes it through Number.
const price = 19.9;
console.log('price:', price.toFixed(2));
console.log('typeof:', typeof price.toFixed(2));
const total = 0.1 + 0.2;
console.log('text:', total.toFixed(2));
console.log('number again:', Number(total.toFixed(2)));
console.log('equal to 0.3:', Number(total.toFixed(2)) === 0.3);
console.log('2.5:', (2.5).toFixed(0));
console.log('3.5:', (3.5).toFixed(0));
console.log('1.005:', (1.005).toFixed(2));
console.log('1 / 3:', (1 / 3).toFixed(5));
node fixed.js
price: 19.90
typeof: string
text: 0.30
number again: 0.3
equal to 0.3: true
2.5: 3
3.5: 4
1.005: 1.00
1 / 3: 0.33333
The rounded text converts back to exactly 0.3, so the round trip through two decimals is a workable way to compare money that a person typed. It is not a general fix: the number of decimals has to fit the data, and 1.005 shows that the rounding follows the stored value rather than the decimal in the source. When the decimals are money, the usual answer is to keep the amount in cents as an integer and divide only for display.
Exercises
- Print typeof 1e21 and 1e21 itself from one-type.js. What does the printed form use instead of twenty-one digits?
- Add console.log(0.1 + 0.7) to precision.js. Is the result exact, and does the same comparison against 0.8 fail?
- Change the tolerance in epsilon.js to a fixed 1e-15 and run the file. Which of the four checks change their answer?
- Print (0.000001).toFixed(2) and (1234.5678).toFixed(2). Does toFixed add a separator for thousands?
- Pass 101 to toFixed in fixed.js. Which error arrives, and which type does the runtime name?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
typeof 4.5print?Answer
number
JavaScript has one number type for integers and fractions, and its name is number.
-
What does this program print?
console.log(0.1 + 0.2 === 0.3);Answer
false
The sum stores a value slightly above three tenths, so it is not equal to the nearest float to 0.3.
-
What does this program print?
console.log((0.1 + 0.2).toFixed(2));Answer
0.30
toFixed returns a string with exactly two decimals, and the stored value rounds to 0.30.
-
What does
toFixedreturn?Answer
A string with the requested number of decimals
The method formats the value as text, which is why Number is needed to get a number back.
-
What is
Number.EPSILON?Answer
The gap between 1 and the next value above 1
It is one unit in the last place at the value 1, which is about 2.22e-16.
-
Why is a comparison with a tolerance better than
===for computed floats?Answer
Because the stored values carry small errors that an exact test rejects
Each operation can add a rounding error, so two values that should be equal can differ in the last bits.
-
What does this program print?
console.log(10 / 4, 10 / 5);Answer
2.5 2
Division always produces a float, and a zero fraction is printed without a decimal point.
-
What does this program print?
console.log(Number.isInteger(42.0));Answer
true
The literal 42.0 is the integer value 42, and the check asks about the value, not about the source text.
-
Which value does
(1.005).toFixed(2)return?Answer
"1.00", because the stored value is slightly below 1.005
The literal 1.005 is stored as a value just under one and five thousandths, so the rounding goes down.
-
How would a program keep money amounts exact?
Answer
Store cents as integers and divide only for display
Integer cents are exact in the safe range, and the division happens once, at the point of printing.
The code on this page was run with Node.js 24.22.1 and linted with ESLint 10.14.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev