Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Number and BigInt

Number.prototype.toPrecision()

Reference for Number.prototype.toPrecision(): formatting a number to a chosen count of significant digits, when it switches to exponent form, and its limits.

toPrecision() formats a number as a string with a given count of significant digits, which are all digits starting from the first non-zero one. Where toFixed() fixes the digits after the decimal point, toPrecision() fixes the total accuracy, so it suits scientific values, measurements and numbers of very different sizes.

It rounds to the requested precision and may switch to exponent notation when the number is very large or very small for that precision.

JavaScript
number.toPrecision(precision)

Try it

JS Significant digits

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Choosing between the formatters

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
toPrecision()
toPrecision(precision)
Name Description
precision Optional. Number of significant digits, an integer from 1 to 100. If omitted (or undefined), the result is the same as toString().

Return value: a string. It uses fixed notation when the decimal exponent of the number is at least -6 and less than precision; otherwise it uses exponent notation such as "1.2e+5".

Exceptions: RangeError if precision is outside 1 to 100.

Usage notes

  • It counts all significant digits, not decimals. (0.00123).toPrecision(2) is "0.0012": leading zeros are not significant.
  • Trailing zeros are kept to reach the requested precision: (1).toPrecision(3) is "1.00".
  • Large integers may become exponent form: (123456).toPrecision(2) is "1.2e+5", because two digits cannot show the whole integer part.
  • Rounding uses the exact stored binary value, as toFixed() does, so values like 1.005 can round down.
  • The result is a string. Convert with Number() to use it in calculations; that drops trailing zeros.
  • NaN and Infinity give "NaN" and "Infinity".
  • Not locale aware. For grouped digits or localized decimals use Intl.NumberFormat with maximumSignificantDigits.
  • Pick the right tool: toFixed() for money-like values with a set number of decimals, toPrecision() for accuracy, toExponential() when you always want scientific notation.

Browser support

Supported in every browser and JavaScript engine.