Standard built-ins
Number.prototype.toPrecision()
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
| Name | Type | Description |
|---|---|---|
| precision | number | Significant digit count 1–100. |
Return Value
string
Formatted Number.
Working Example
Example Snippetjavascript
console.log((12.5).toPrecision(2));