Borislav Hadzhiev
Last updated: Jul 25, 2022
Check out my new book
Use the toFixed()
method to round a number to 2 decimal places, e.g.
const result = num.toFixed(2)
. The toFixed
method will round and format the
number to 2 decimal places.
// ✅ round number to 2 decimal places const num1 = 13.5748; const result1 = num1.toFixed(2); console.log(result1); // 👉️ 13.57 console.log(typeof result1); // 👉️ string // 👇️ if you need to convert str to float const final = parseFloat(result1); console.log(final); // 👉️ 13.57 // ---------------------------------------- // ✅ round number wrapped in string to 2 decimal places // 👇️ if the value is a string // call parseFloat to convert it to a number first const str1 = '13.5758'; const result2 = parseFloat(str1).toFixed(2); console.log(result2); // 👉️ 13.58 console.log(typeof result2); // 👉️ string // ---------------------------------------- // 👇️ Convert string back to a number const num2 = 13.1999; const result3 = Number(num2.toFixed(2)); console.log(result3); // 👉️ 13.2 console.log(typeof result3); // 👉️ number
The first example uses the Number.toFixed method to round a number to 2 decimal places.
const num1 = 13.5748; const result1 = num1.toFixed(2); console.log(result1); // 👉️ 13.57 console.log(typeof result1); // 👉️ string // 👇️ if you need to convert str to float const final = parseFloat(result1); console.log(final); // 👉️ 13.57
The only parameter the method takes is the number of digits to appear after the decimal point.
toFixed
method returns a string representation of the number.In the second example, we have a floating-point number that is wrapped in a string.
const str1 = '13.5758'; const result2 = parseFloat(str1).toFixed(2); console.log(result2); // 👉️ 13.58 console.log(typeof result2); // 👉️ string // 👇️ if you need to convert str to float const final = parseFloat(result2); console.log(final); // 👉️ 13.58
We used the
parseFloat
function to convert the string to a number because toFixed
can only be called
on numbers.
You can use the Number
object or the parseFloat()
function to convert the
string that the toFixed
method returns into a floating-point number.
const num2 = 13.1999; const result3 = Number(num2.toFixed(2)); console.log(result3); // 👉️ 13.2 console.log(typeof result3); // 👉️ number const final = parseFloat(num2.toFixed(2)); console.log(final); // 👉️ 13.2
The number 1.00
is the same as 1
, so the trailing zeros get dropped when the
value is converted to a number.
Note that floating-point numbers don't represent all decimals precisely in binary, which can lead to inconsistent results.
console.log(0.1 + 0.2 === 0.3); // 👉️ false const n1 = (13.555).toFixed(2); console.log(n1); // 👉️ 13.55 const n2 = (13.5551).toFixed(2); console.log(n2); // 👉️ 13.56
In the first example 0.1
+ 0.2
equals 0.30000000000000004
instead of
0.3
. This is because the binary floating-point format cannot accurately
represent numbers like 0.1
or 0.2
.
The code gets rounded to the nearest number, resulting in a rounding error.
In the second example, we would expect to get 13.56
back, but instead the
number gets rounded down to 13.55
.