Skip to content
BasicsChapter 9 / 47Beginner4 min read

Numbers & Math

A guide to JavaScript numbers and Math, the floating point problem, toFixed, Math.round, random numbers and Intl.NumberFormat.

On this page

1. What are numbers and Math?

Under the hood, every number is a 64-bit floating point value. That is why 5 and 5.0 are exactly the same. There are also some special values:

ValueWhen you get it
Infinity1 / 0
-Infinity-1 / 0
NaNInvalid math, like "abc" * 2
Number.MAX_SAFE_INTEGER9007199254740991, precision breaks above this

2. Why does it matter?

Prices, discounts, ratings, pagination, all of it is numbers. If you do not know the floating point problem, you are surprised to see 0.30000000000000004 as the answer to 0.1 + 0.2, and wrong amounts end up in a bill.

Without understandingWith understanding
Adding rupees as decimalsKeeping paisa as integers, dividing at the end
Formatting numbers by handCurrency format with Intl.NumberFormat
Strange formulas for randomThe standard pattern of Math.floor and Math.random
Counting zeros in big numbersNumeric separators like 1_000_000

3. Where is it used?

Real useTool
Showing a price with 2 decimalstoFixed(2) or Intl.NumberFormat
Rounding a ratingMath.round()
Total pagesMath.ceil(total / perPage)
OTP or diceMath.random() + Math.floor()
The largest valueMath.max(...values)
Number checksNumber.isInteger(), Number.isNaN()

4. How does it work?

The computer stores numbers in binary. Decimals like 0.1 cannot be written exactly in binary, so a tiny difference is left over.

The journey of 0.1 + 0.2
0.1 in binary, slightly approximate
0.2 in binary, slightly approximate
Add both, the small errors add up
0.30000000000000004

5. Code example

Floating point and a safe comparison:

js
console.log(0.1 + 0.2);
console.log(0.1 + 0.2 === 0.3);

const close = Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON;
console.log(close);

const pricesInPaisa = [1050, 2099, 350];
const totalPaisa = pricesInPaisa.reduce((sum, p) => sum + p, 0);
console.log(totalPaisa / 100);

Output: 0.30000000000000004, false, true, 34.99.

Math methods:

js
console.log(Math.round(4.5));
console.log(Math.floor(4.9));
console.log(Math.ceil(4.1));
console.log(Math.trunc(-4.7));
console.log(Math.max(3, 9, 1));
console.log(Math.abs(-12));
console.log(2 ** 10);

const dice = Math.floor(Math.random() * 6) + 1;
console.log(dice);

Output: 5, 4, 5, -4, 9, 12, 1024, and a random number from 1 to 6.

Formatting:

js
const salary = 1_250_000;

console.log((19.987).toFixed(2));

const pkr = new Intl.NumberFormat("en-PK", {
  style: "currency",
  currency: "PKR",
});
console.log(pkr.format(salary));

toFixed(2) gives the string "19.99". Intl.NumberFormat adds the currency and commas according to the locale.

How does the code work?

ItemMeaning
Number.EPSILONA tiny margin for two numbers being "almost equal"
Math.floor / Math.ceilDown / up to the nearest integer
Math.truncCuts off the decimal part
Math.random()A random decimal from 0 up to (not including) 1
1_250_000A numeric separator, only for easier reading
toFixed(2)A string with 2 decimals

Interview tip

"Why is 0.1 + 0.2 !== 0.3?" Answer: JavaScript uses IEEE 754 double precision floats. In binary, 0.1 and 0.2 are not exact, so a small rounding error appears. This is not only JavaScript, most languages do it.

6. Common mistakes

Mistake: Treating the result of toFixed as a number

Why? toFixed returns a string, so using plus on it joins text.

Better: Do calculations with numbers, and use toFixed or Intl.NumberFormat only for display.

Mistake: Comparing decimals directly with ===

Why? Because of floating point error, 0.1 + 0.2 === 0.3 gives false.

Better: Work in integers (like paisa) or use a tolerance check with Number.EPSILON.

Mistake: Using Math.random for security

Why? Math.random is not cryptographically secure, so its numbers can be predicted.

Better: Use crypto.getRandomValues or crypto.randomUUID for tokens or passwords.

7. What to learn before and after

Final quick revision

PointDetail
One number typeBoth integers and decimals, a 64-bit float
Floating point0.1 + 0.2 is not exactly 0.3
MoneyCalculate in integers
Roundinground, floor, ceil, trunc
Random intMath.floor(Math.random() * n) + 1
FormattingIntl.NumberFormat, toFixed gives a string