Learn how to format a number with two decimals in JavaScript.
Format a Number with Two Decimals
You can use the toFixed() method to format a number to only show two decimals. Note that the result is rounded (shows 5.57 instead of 5.56):
Example
js
let num = 5.56789;
let n = num.toFixed(2); // 5.57
If you want to display three decimals, just change the number to 3:
Example
js
let num = 5.56789;
let n = num.toFixed(3); // 5.568
Tip: Learn more about the toFixed() method in the JavaScript Reference.