X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

JavaScript toFixed() Method

JavaScript Number Reference JavaScript Number Reference

Example

Convert a number into a string, keeping only two decimals:

var num = 5.56789;
var n = num.toFixed(2);

The result of n will be:


Try it yourself »


Definition and Usage

The toFixed() method converts a number into a string, keeping a specified number of decimals.

Note: if the desired number of decimals are higher than the actual number, nulls are added to create the desired decimal length.


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The toFixed() method is supported in all major browsers.


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)

Return Value

Type Description
String The number, with the exact number of decimals

Technical Details

JavaScript Version: 1.5


More Examples

Example

Convert a number, without keeping any decimals:

var num = 5.56789;
var n = num.toFixed();

The result of n will be:


Try it yourself »

Example

Convert a number which has fewer decimal places than requested:

var num = 5.56789;
var n = num.toFixed(10);

The result of n will be:


Try it yourself »


JavaScript Number Reference JavaScript Number Reference

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates FREE Website Templates Free HTML Templates

SHARE THIS PAGE

facebook