To round a number to 2 decimal places in JavaScript, call the toFixed()
method on the number, i.e., num.toFixed(2)
. toFixed()
will round and format the number to 2 decimal places.
For example:
const num = 5.3281;
const result = num.toFixed(2);
console.log(result); // 5.33
const num2 = 3.1417
const result2 = num2.toFixed(2);
console.log(result2); // 3.14
The toFixed()
method takes a number F
and returns a string representation of a number with F
number of digits after the decimal points. F
here is determined by the first argument passed to toFixed()
, the fractionDigits
argument.
const num = 5.3281;
console.log(num.toFixed(0)); // 5
console.log(num.toFixed(1)); // 5.3
console.log(num.toFixed(2)); // 5.33
console.log(num.toFixed(3)); // 5.328
console.log(num.toFixed(4)); // 5.3281
console.log(num.toFixed(5)); // 5.32810
Parse result of toFixed()
to number
Remember, toFixed()
returns a string representation:
const num = 5.3281;
const result = num.toFixed(2);
console.log(result); // 5.33
console.log(typeof result); // string
But we can always convert the result to a number with the Number()
constructor:
const num = 5.3281;
const result = Number(num.toFixed(2));
console.log(result); // 5.33
console.log(typeof result); // number
If the string has trailing zeroes, they will be removed in the conversion:
const num = 9.999999;
const strResult = num.toFixed(2);
const result = Number(strResult);
console.log(strResult); //10.00
console.log(result); // 10
The trailing zeros after the decimal point don’t change the value of a number, so 10.00
is the same as 10
or 10.00000000
console.log(10.00 === 10); // true
console.log(10.00000000 == 10); // true
Round decimal string to 2 decimal places
Sometimes the input might be stored as a string. In this case, we’ll first need to convert the number to a float with the parseFloat()
function before using toFixed()
to round it to 2 decimal places.
For example:
const numStr = '17.23593';
// 👇 convert string to float with parseFloat()
const num = parseFloat(numStr);
const result = num.toFixed(2); // 17.24
console.log(result);
Not all decimal numbers can be represented precisely in binary, so there are some rounding errors in JavaScript’s floating-point number system. For example:
console.log(44.85 * 0.1); // 4.485
console.log(45.85 * 0.1); // 4.585
console.log(46.85 * 0.1); // 4.6850000000000005 (?)
In this example, 46.85 x 0.1
equals 4.6850000000000005
because 46.85
can’t be accurately represented in binary floating-point format.
console.log((1.415).toFixed(2)); // 1.42
console.log((1.215).toFixed(2)); // 1.22
console.log((1.015).toFixed(2)); // 1.01 (?)
Like in the first one, here 1.015
is rounded down to 2 decimal places as 1.01
instead of 1.02
, because 1.015
also can’t be represented accurately in the binary number system.
One of the most popular examples of this flaw is the classic 0.1 + 0.2
:
console.log(0.1 + 0.2 === 0.3); // false
console.log(0.1 + 0.2); // 0.30000000000000004
You can find this and more of other interesting JavaScript quirks in our Crazy JS Book.
11 Amazing New JavaScript Features in ES13
This guide will bring you up to speed with all the latest features added in ECMAScript 13. These powerful new features will modernize your JavaScript with shorter and more expressive code.