English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
toPrecision()The method's purpose is to return a string representing a number with a specified precision.
number.toPrecision(precision)
var num = 5.123456; num.toPrecision(3);Test and see‹/›
The toPrecision() method is fully supported by all browsers:
Method | |||||
toPrecision() | Is | Is | Is | Is | Is |
Parameter | Description |
---|---|
precision | (Optional) an integer used to specify the number of significant digits |
Return value: | a string representing a number formatted to a specified precision |
---|---|
Exception cases: | If the precision is not1to10throws a RangeError if the precision is not between |
JavaScript Version: | ECMAScript 3 |
Format a number to a specified length:
var num = 5.123456; num.toPrecision();// 5.123456 num.toPrecision(1); // 5 num.toPrecision(2); // 5.1 num.toPrecision(3); // 5.12 num.toPrecision(4); // 5.123 num.toPrecision(10); // 5.123456000Test and see‹/›