Skip to main content
Version: 3.27.2

Number.prototype.toPrecision()

The toPrecision() method returns a string representing the Number object to the specified precision.

Syntax

toPrecision()
toPrecision(precision)

Parameters

  • precision optional
    • : An integer specifying the number of significant digits.

Return value

A string representing a Number object in fixed-point or exponential notation rounded to precision significant digits. See the discussion of rounding in the description of the Number.prototype.toFixed() method, which also applies to toPrecision().

If the precision argument is omitted, behaves as Number.prototype.toString(). If the precision argument is a non-integer value, it is rounded to the nearest integer.

Exceptions

  • RangeError
    • : If precision is not between 1 and 100 (inclusive), a RangeError is thrown. Implementations are allowed to support larger and smaller values as well. ECMA-262 only requires a precision of up to 21 significant digits.