totn JavaScript

JavaScript: Number toPrecision() method

This JavaScript tutorial explains how to use the Number method called toPrecision() with syntax and examples.

Description

In JavaScript, toPrecision() is a Number method that is used to convert a number to a specified precision (rounding the result where necessary) and return its value as a string. Because toPrecision() is a method of the Number object, it must be invoked through a particular instance of the Number class.

Syntax

In JavaScript, the syntax for the toPrecision() method is:

number.toPrecision([significantDigits]);

Parameters or Arguments

significantDigits
Optional. It is the number of significant digits to display in the result. If this parameter is omitted, the toPrecision() method will simply convert the number to a string, as is.

Returns

The toPrecision() method converts a number to a specified precision with the indicated number of significant digits (rounding the result where necessary) and then returns its value as a string.

If more significant digits are required than was present in the original number, the toPrecision() method will pad the result with 0's, accordingly.

The toPrecision() method can handle both fixed-point notation numbers as well as exponential notation numbers.

Note

  • The toPrecision() method will round the resulting value if necessary.
  • The toPrecision() method will pad the resulting value with 0's if there are not enough significant digits.
  • The toPrecision() method does not change the value of the original number.

Example

Let's take a look at an example of how to use the toPrecision() method in JavaScript.

For example:

var totn_number = 8.7654321;

console.log(totn_number.toPrecision());
console.log(totn_number.toPrecision(1));
console.log(totn_number.toPrecision(2));

In this example, we have declared a variable called totn_number that is assigned the value of 8.7654321. We have then invoked the toPrecision() method of the totn_number to convert the number to the specified precision.

We have written the output of the toPrecision() method to the web browser console log, for demonstration purposes, to show what the toPrecision() method returns.

The following will be output to the web browser console log:

8.7654321
9
8.8

In this example, the first output to the console log returned the string value "8.7654321" which is the number 8.7654321 converted merely to a string since no signfiicantDigits parameter was provided.

The second output to the console log returned the string value "9" which is the number 8.7654321 with 1 significant digit. Notice that the result has been rounded.

The third output to the console log returned the string value "8.8" which is the number 8.7654321 with 2 significant digits. Notice that the result has been rounded.

Specifying an Exponential Notation

The toPrecision() method can also handle numbers that are written in exponential notation.

For example:

var totn_number = 1.23456789e+5;

console.log(totn_number.toPrecision(3));
console.log(totn_number.toPrecision(5));

The following will be output to the web browser console log:

1.23e+5
1.2346e+5

In this example, the output to the console log returned the string value "1.23e+5" which is the value 1.23456789e+5 with 3 significant digits.

The second output to the console log returned the string value "1.2346e+5" which value for 1.23456789e+2 with 5 significant digits. Notice that the result has been rounded.

Padding the Decimal Places

Finally, let's explore how the toPrecision() method pads the result with 0's if there are not enough significant digits in the original number.

For example:

var totn_number = 123.4

console.log(totn_number.toPrecision(5));
console.log(totn_number.toPrecision(6));

The following will be output to the web browser console log:

123.40
123.400

In this example, the first output to the console log returned the string value "123.40" which is the number 123.4 padded to a precision of 5 with 0's.

The second output to the console log returned the string value "123.400" which is the number 123.4 padded to a precision of 6 with 0's.