# Using Number toPrecision() method to coding in Javascript/JS

This JavaScript tutorial explains how to use the Number approach referred to as toPrecision() with syntax and examples.

## Description

In JavaScript, toPrecision() is a Number approach that is used to convert a range to a detailed precision (rounding the result the place necessary) and return its value as a string. Because toPrecision() is a technique of the Number object, it should be invoked via a unique occasion of the Number class.

## Syntax

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

``number.toPrecision([significantDigits]);``

Parameters or Arguments

significantDigits Optional. It is the variety of sizeable digits to show in the result. If this parameter is omitted, the toPrecision() approach will in reality convert the variety to a string, as is.

## Returns

The toPrecision() approach converts a range to a special precision with the indicated quantity of considerable digits (rounding the end result the place necessary) and then returns its fee as a string.

If extra full-size digits are required than was present in the original number, the toPrecision() method will pad the result with 0’s, accordingly.

The toPrecision() approach can deal with both fixed-point notation numbers as nicely as exponential notation numbers.

## Note

The toPrecision() method will round the resulting cost if necessary. The toPrecision() approach will pad the resulting value with 0’s if there are now not enough widespread digits. The toPrecision() method does no longer trade the price of the original number.

## Example

Let’s take a appear at an example of how to use the toPrecision() approach 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 referred to as totn_number that is assigned the cost of 8.7654321. We have then invoked the toPrecision() approach of the totn_number to convert the range to the targeted precision.

We have written the output of the toPrecision() technique to the web browser console log, for demonstration purposes, to exhibit 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 price “8.7654321” which is the wide variety 8.7654321 transformed in simple terms to a string due to the fact that no signfiicantDigits parameter was provided.

The 2nd output to the console log back the string price “9” which is the range 8.7654321 with 1 sizeable digit. Notice that the end result has been rounded.

The 1/3 output to the console log lower back the string cost “8.8” which is the variety 8.7654321 with two enormous digits. Notice that the end result has been rounded.

Specifying an Exponential Notation

The toPrecision() approach can also cope with 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 internet browser console log:

``````1.23e+5
1.2346e+5``````

In this example, the output to the console log back the string cost “1.23e+5” which is the fee 1.23456789e+5 with three considerable digits.

The 2d output to the console log back the string value “1.2346e+5” which fee for 1.23456789e+2 with 5 extensive digits. Notice that the result has been rounded.

Finally, let’s discover how the toPrecision() method pads the result with 0’s if there are no longer ample giant digits in the unique 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 back the string value “123.40” which is the range 123.4 padded to a precision of 5 with 0’s.

The second output to the console log back the string cost “123.400” which is the number 123.4 padded to a precision of 6 with 0’s.