X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY ANGULAR BOOTSTRAP 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 »

More "Try it Yourself" examples below.


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

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: 1.5


Examples

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 HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE

facebook