Number.prototype.toPrecision
Returns a string representing the Number object to the specified precision
Syntax
number.toPrecision(precision?)Parameters
| Parameter | Type | Description |
|---|---|---|
| precision | number | An integer specifying the number of significant digits (1-100) |
Return Value
A string representing the number to the specified precision
Examples
const num = 123.456;
console.log(num.toPrecision(5)); // '123.46'
console.log(num.toPrecision(2)); // '1.2e+2'console.log((0.00123).toPrecision(2)); // '0.0012'
console.log((123.456).toPrecision(7)); // '123.4560'const pi = Math.PI;
console.log(pi.toPrecision(4)); // '3.142'
console.log(pi.toPrecision(10)); // '3.141592654'Understanding Number.prototype.toPrecision
The Number.prototype.toPrecision method in JavaScript returns a string representing the Number object to the specified precision. It belongs to the Number object and is one of the most widely used methods for working with number values in modern JavaScript and TypeScript applications.
The method signature is number.toPrecision(precision?). It accepts 1 parameter: precision. When called, it returns a string representing the number to the specified precision. Understanding when and how to use toPrecision() helps you write more expressive, readable code.
Common use cases for Number.prototype.toPrecision include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like number-tofixed, number-toexponential, number-tostring, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Number.prototype.toPrecision is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Number.prototype.toFixedFormats a number using fixed-point notation with the specified number of decimal places
Number.prototype.toExponentialReturns a string representing the Number object in exponential notation
Number.prototype.toStringReturns a string representing the specified Number object in the specified radix (base)
More Number Methods
Other methods in the Number object
Related Tools
More Number Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.