let num = 5.56789;
let n = num.toFixed();
Try it Yourself »
let num = 5.56789;
let n = num.toFixed(2);
Try it Yourself »
More examples below
The toFixed()
method converts a number to a string.
The toFixed()
method rounds the string to a specified number of decimals.
If the number of decimals are higher than in the number, zeros are added.
number.toFixed(
x)
Parameter | Description |
x | Optional. Number of decimals. Default is 0 (no decimals) |
Type | Description |
A string | The representation of a number with (or without) decimals. |
toFixed()
is an ECMAScript3 (ES3) feature.
ES3 (JavaScript 1999) is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!