English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript toPrecision() Method

 JavaScript Number Object

toPrecision()The method's purpose is to return a string representing a number with a specified precision.

Syntax:

number.toPrecision(precision)
var num = 5.123456;
num.toPrecision(3);
Test and see‹/›

Browser Compatibility

The toPrecision() method is fully supported by all browsers:

Method
toPrecision()IsIsIsIsIs

Parameter Value

ParameterDescription
precision(Optional) an integer used to specify the number of significant digits

Technical Details

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

More Examples

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.123456000
Test and see‹/›

 JavaScript Number Object