JavaScript toFixed() Method
Example
Convert a number into a string, rounding the number to keep only two decimals:
var num = 5.56789;
var n = num.toFixed(2);
Try it Yourself »
More "Try it Yourself" examples below.
Definition and Usage
The toFixed() method converts a number into a string, rounding to a specified number of decimals.
Note: if the desired number of decimals are higher than the actual number, zeros are added to create the desired decimal length.
Browser Support
Method | |||||
---|---|---|---|---|---|
toFixed() | Yes | Yes | Yes | Yes | Yes |
Syntax
number.toFixed(x)
Parameter Values
Parameter | Description |
---|---|
x | Optional. The number of digits after the decimal point. Default is 0 (no digits after the decimal point) |
Technical Details
Return Value: | A String, representing a number, with the exact number of decimals |
---|---|
JavaScript Version: | ECMAScript 3 |
More Examples
Example
Convert a number, rounding to nearest integer (no decimals):
var num = 5.56789;
var n = num.toFixed();
Try it Yourself »
Example
Convert a number which has fewer decimal places than requested:
var num = 5.56789;
var n = num.toFixed(10);
Try it Yourself »