How to format numbers to two decimal places

Learn how to format numbers to two decimal places in JavaScript.

to format the number to two decimal places

You can use toFixed() The method formats the number to display only two decimal places. Note that the result is rounded (displaying 5.57 instead of 5.56):

Example

let num = 5.56789;
let n = num.toFixed(2);  // 5.57

Try It Yourself

If you want to display three decimal places, just change the number to 3:

Example

let num = 5.56789;
let n = num.toFixed(3);  // 5.568

Try It Yourself

Related Pages

Reference Manual:JavaScript Number toFixed() Method