Latest web development tutorials

JavaScript toPrecision () method

Number Object Reference JavaScript Number Object

Examples

Digital format for the specified length:

var num = new Number(13.3714);
var n=num.toPrecision(2);

n output:


try it"

Definition and Usage

When toPrecision () method in the value of an object exceeds a specified number of converts to exponential notation.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support toPrecision () method


grammar

number.toPrecision(x)

Parameter Value

参数 描述
x 必需。规定必须被转换为指数计数法的最小位数。该参数是 1 ~ 21 之间(且包括 1 和 21)的值。有效实现允许有选择地支持更大或更小的 num。如果省略了该参数,则调用方法 toString(),而不是把数字转换成十进制的值。

return value

类型 描述
String 指定精度的数字格式

technical details

JavaScript version: 1.5


More examples

Examples

Use different binary digital format for the specified length:

var num = new Number(13.3714);
var a = num.toPrecision();
var b = num.toPrecision(2);
var c = num.toPrecision(3);
var d = num.toPrecision(10);

a, b, c, and d output:


try it"


Number Object Reference JavaScript Number Object