Latest web development tutorials
 

JavaScript toFixed() Method

< 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