JavaScript Number toFixed()
Examples
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
Description
The toFixed()
method converts a number to a string.
The toFixed()
method rounds the string to a specified number of decimals.
Note
If the number of decimals are higher than in the number, zeros are added.
Syntax
number.toFixed(x)
Parameters
Parameter | Description |
x | Optional. Number of decimals. Default is 0 (no decimals) |
Return Value
Type | Description |
A string | The representation of a number with (or without) decimals. |
More Examples
Browser Support
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 |