JavaScript toPrecision() Method Number.toPrecision() method

This tutorial explores the toPrecision() method, part of the JavaScript's Number object. The toPrecision() method allows you to return a string representation of a number and control the number of significant digits displayed.

The precision argument must be at least 1, and at most 21. An error will be returned if an out-of-range number is passed as argument. While a string representation of the number is returned, no conversion occurs beforehand: to apply the toPrecision() method on a number-containing string, extract first the numerical value using, for example, the parseFloat() function.

The toPrecision() method takes all digits into account, preceding and following the decimal point.

To control the number of digits appearing after the decimal point, use instead the toFixed() method.

Using the toPrecision() Method without Argument

Used without argument, the toPrecision() method performs the same operation as the toString() method, and converts to base 10 the number on which it is applied. (The "0x" prefix tells JavaScript that the number is in base 16.)

var numex = 0xF;
alert( numex.toPrecision() );

The result returned is a string representation of our hexadecimal number converted to base 10 (hexadecimal F corresponds to 10 in base 10). JavaScript toPrecision() method used without argument

Using the toPrecision() Method with an Argument

The optional argument passed to the toPrecision() method represents the number of digits (precision) we require. If needed, the toPrecision() method will also round up or down the number on which it is applied.

var numex1 = 3.14;
var numex2 = 3.16;

alert( numex1.toPrecision(2) +"\n"+ numex2.toPrecision(2) );

The JavaScript code above produces the following result. (Notice how each number was also rounded to match the precision requested.) JavaScript toPrecision() method with 1 argument

In our demonstration script, we passed 2 as precision argument; passing 1 as argument would have returned 3 as result for both number variables.

Somewhat counter-intuitively, 2 digits precision includes numbers before the decimal point.

The script below assigns a larger value to our numerical variable, and passes 3 as argument to the toPrecision() method.

var numex = 123.45;
alert( numex.toPrecision(3) );

The result we obtained illustrates how toPrecision() takes into account all digits, not just those following the decimal point: JavaScript toPrecision() method for larger number

Test the Number.toPrecision() Method

Interactively test the Number.toPrecision() method by editing the JavaScript code below and clicking the Test Number.toPrecision() method button.

Browser support for JavaScript Number toPrecision() method
Internet Explorer supports the JavaScript Number toPrecision() methodFirefox supports the JavaScript Number toPrecision() methodSafari supports the JavaScript Number toPrecision() methodOpera supports the JavaScript Number toPrecision() method