JavaScript Number toFixed()
Examples
let num = 5.56789;
let n = num.toFixed();
Try it Yourself »
let n = num.toFixed();
let num = 5.56789;
let n = num.toFixed(2);
Try it Yourself »
let n = num.toFixed(2);
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)
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 ECMAScript1 (JavaScript 1997) feature.
It is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |