JavaScript toPrecision method | format a number to a specific precision or length

JavaScript toPrecision is used to format a number to a specific precision or length. Basically its specified length of number.

Note: Js toPrecision method returns a string representing the Number object to the specified precision/length.

Syntax

Example of JavaScript toPrecision method

Example of How to JavaScript Format a number into a specified length.

Output:

JavaScript toPrecision method

What if JavaScript decimal precision more than then actual number?

At this condition a decimal point and nulls (zero) are added to create the specified length. See below example:-

Output: 98.33000

Do comment if you have any doubts and suggestions on this tutorial.

Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.