Latest web development tutorials

JavaScript toFixed () method

Number Object Reference JavaScript Number Object

Examples

After the number is converted to a string, the result of a designated decimal digits:

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

n output:


try it"

Definition and Usage

toFixed () method of the Number rounded to the specified number of decimal places.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support toFixed () method


grammar

number.toFixed( x )

Parameter Value

参数 描述
x 必需。规定小数的位数,是 0 ~ 20 之间的值,包括 0 和 20,有些实现可以支持更大的数值范围。如果省略了该参数,将用 0 代替。

return value

类型 描述
String 小数点后有固定的 x 位数字

technical details

JavaScript version: 1.5


More examples

Examples

A number that does not leave any fractional:

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

n output:


try it"

Examples

A conversion smaller than the specified number of digits after the decimal point:

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

n output:

var num = 5.56789; document.write(num.toFixed(10));

try it"


Number Object Reference JavaScript Number Object