Skip to content
Laviyx Learn

Try

JavaScript course menu

JavaScript Numbers and Math: Arithmetic, Rounding, Random Numbers and Precision

Work with numbers in JavaScript: arithmetic operators, the remainder operator, decimal precision (0.1 + 0.2), rounding with Math.round, toFixed and Math.floor, random numbers, Math.max and Math.min, NaN and Infinity, safe integers and BigInt, and formatting with toLocaleString.

Beginner8 min readUpdated
On this page

JavaScript has one main number type, number, for both whole numbers and decimals. This lesson covers calculations, rounding, random numbers and the traps to avoid.

Arithmetic#

JavaScript
console.log(10 + 3);
console.log(10 - 3);
console.log(10 * 3);
console.log(10 / 3);
console.log(10 % 3);   // remainder
console.log(2 ** 10);  // power
Output
13
7
30
3.3333333333333335
1
1024
  • / always gives an exact (decimal) result; there is no separate integer division.
  • % gives the remainder: 10 % 3 is 1. It is handy for checking even numbers (n % 2 === 0) and wrapping around (index % length).
  • ** raises to a power.

Normal maths precedence applies: *, / and % happen before + and -. Use parentheses to be clear: (a + b) * c.

Decimal precision#

Numbers are stored in binary floating point (the IEEE 754 standard, used by most languages). Some decimals cannot be stored exactly:

JavaScript
console.log(0.1 + 0.2);
console.log(0.1 + 0.2 === 0.3);
Output
0.30000000000000004
false

This is not a JavaScript bug; Python, Java and C++ behave the same way. For money, a common approach is to work in the smallest unit (paise or cents) as whole numbers, and only format as rupees for display.

JavaScript
const pricePaise = 1999;   // ₹19.99
const totalPaise = pricePaise * 3;
console.log(totalPaise / 100);
Output
59.97

Rounding#

JavaScript
const value = 4.567;

console.log(Math.round(value));   // nearest whole number
console.log(Math.floor(value));   // down
console.log(Math.ceil(value));    // up
console.log(Math.trunc(-4.567));  // remove the decimal part
console.log(value.toFixed(2));    // a STRING with 2 decimals
console.log(Math.round(value * 100) / 100);  // a number with 2 decimals
Output
5
4
5
-4
4.57
4.57

toFixed() returns a string, which is ideal for display. For further calculations, round with Math.round(value * 100) / 100 instead.

The Math object#

JavaScript
console.log(Math.max(4, 19, 7));
console.log(Math.min(4, 19, 7));
console.log(Math.abs(-12));
console.log(Math.sqrt(81));
console.log(Math.PI);
Output
19
4
12
9
3.141592653589793

Random numbers#

Math.random() returns a decimal from 0 (inclusive) up to, but not including, 1. To get a whole number in a range:

JavaScript
// A random whole number from 1 to 6, like a dice roll
const roll = Math.floor(Math.random() * 6) + 1;
console.log(roll);

The output changes every time, so it is not shown here. Math.random() is fine for games and sampling but not for passwords or security tokens; use crypto.getRandomValues() or crypto.randomUUID() for those.

NaN and Infinity#

JavaScript
console.log(0 / 0);
console.log("abc" * 2);
console.log(10 / 0);
console.log(Number.isNaN("abc" * 2));
console.log(NaN === NaN);
Output
NaN
NaN
Infinity
true
false
  • NaN ("not a number") is the result of a calculation that makes no sense. It spreads: any maths with NaN gives NaN.
  • NaN is not equal to anything, not even itself, so test with Number.isNaN(value).
  • Dividing by zero gives Infinity (or -Infinity), not an error.

Large integers and BigInt#

Numbers can represent whole numbers exactly only up to Number.MAX_SAFE_INTEGER (about 9 quadrillion). Beyond that, use BigInt, written with an n suffix:

JavaScript
console.log(Number.MAX_SAFE_INTEGER);
console.log(9007199254740992 + 1);
console.log(9007199254740992n + 1n);
Output
9007199254740991
9007199254740992
9007199254740993n

The plain number calculation gives the wrong answer silently; BigInt gives the exact one. BigInts cannot be mixed with normal numbers in arithmetic without converting first.

Formatting numbers#

toLocaleString formats numbers for a language and region, including Indian digit grouping and currencies:

JavaScript
const amount = 1234567.891;
console.log(amount.toLocaleString("en-IN"));
console.log(amount.toLocaleString("en-US"));
console.log(amount.toLocaleString("en-IN", { style: "currency", currency: "INR" }));
Output
12,34,567.891
1,234,567.891
₹12,34,567.89

Practice#

Practice exercise

A restaurant bill is ₹1,850 for 3 people, plus a 10% tip. Print the total with the tip, each person's share rounded to 2 decimal places, and whether the rounded total is an even number of rupees.

Show solution
JavaScript
const bill = 1850;
const total = bill * 1.1;
const share = total / 3;

console.log("Total:", total);
console.log("Each pays:", share.toFixed(2));
console.log("Total is even:", Math.round(total) % 2 === 0);
Output
Total: 2035.0000000000002
Each pays: 678.33
Total is even: false

The total shows the precision issue from earlier: 1.1 cannot be stored exactly, so the result is a tiny bit more than 2035. Rounding before display or comparison fixes it, which is why Math.round(total) is used for the even check (2035 is odd).

Key takeaway#

Finished this lesson?