Standard built-ins

Number.prototype.toPrecision()

Try the free JavaScript compiler

Formats a Number with a chosen count of significant digits from 1 to 100. Omitted precision uses ordinary number-to-string. May use exponent notation. Out-of-range digit counts can throw RangeError. Result is text; binary representation influences rounding.

Formal Syntax & Structure

javascript syntaxjavascript
number.toPrecision(precision?)

Parameters & Attributes

NameTypeDescription
precisionnumberSignificant digit count 1–100.

Return Value

string

Formatted Number.

Working Example

Example Snippetjavascript
console.log((12.5).toPrecision(2));
JavaScript Reference