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 stringThe representation of a number with (or without) decimals.


More Examples

Round to 10 decimals

let num = 5.56789;
let n = num.toFixed(10);
Try it Yourself »

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

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.