Number.prototype.toPrecision()
Formats a number to a set count of significant digits and returns it as a string.
toPrecision()
toPrecision(precision)Parameters
| Parameter | Type | Description |
|---|---|---|
$precisionoptional | number | An integer count of significant digits. Must be between 1 and 100 inclusive. |
Return value
stringA string representing the number with the given count of significant digits. Scientific notation is used when the exponent is greater than or equal to `precision`, or less than -6. With no argument the result matches `Number.prototype.toString()`.
Errors
Throws a RangeError if precision is not between 1 and 100, and a TypeError if called on a value that is not a number.
Where toFixed() counts digits after the decimal point, toPrecision() counts total significant digits, starting from the first non-zero one. That makes it the right tool for values whose magnitude varies: (0.000123).toPrecision(2) is "0.00012" and (123456).toPrecision(3) is "1.23e+5" — both keep two or three meaningful digits regardless of scale.
It switches notation on you
This is the trip-up. The output is fixed-point only while the number fits; once the exponent reaches precision, or drops below -6, you get exponential notation instead. A formatter that looks fine on 42.7 can emit "1.23e+5" the moment a bigger value arrives. If a stable, non-scientific shape matters, use toFixed() or toLocaleString() with maximumSignificantDigits.
When to reach for it
Sensor readings, scientific results, and anything where trailing zeros carry meaning: (1.5).toPrecision(5) is "1.5000", recording that five digits were measured. It is also useful for compact log output where you want a consistent information density across values that span several orders of magnitude.
Called with no argument at all, it simply behaves like toString().
Example
When to use it
- Print sensor readings to three significant figures regardless of scale
- Keep trailing zeros that record measurement precision
- Produce compact, uniform-width numbers in a log line
Frequently asked questions
What is the difference between toPrecision() and toFixed()?
toPrecision() counts total significant digits from the first non-zero digit, while toFixed() counts digits after the decimal point. For 0.001234, toPrecision(3) gives "0.00123" and toFixed(3) gives "0.001".
How do I stop toPrecision() from returning scientific notation?
Pass the result through Number() and back to a string, or use toLocaleString() with maximumSignificantDigits, which never switches to exponent form for ordinary magnitudes.
Why does toPrecision(5) add trailing zeros to 1.5?
Because significant-digit formatting pads to the requested count to record how precisely the value is known; "1.5000" states five significant digits, which is the point of the method.
More examples
Significant digits versus decimal places
toPrecision() keeps three meaningful digits at every scale; toFixed() keeps three digits after the point, which loses all detail for small values and pads noise onto large ones.
const values = [0.001234, 1.234, 1234];
for (const v of values) {
console.log(v, '->', v.toPrecision(3), '|', v.toFixed(3));
}
// 0.001234 -> "0.00123" | "0.001"
// 1.234 -> "1.23" | "1.234"
// 1234 -> "1.23e+3" | "1234.000"Avoiding the exponential surprise
Round-tripping through Number() gives the plain decimal form when one exists, which is usually what a dashboard wants even though it discards the trailing-zero information.
function sigFigs(n, digits) {
const s = n.toPrecision(digits);
// Re-render through Number to drop exponent form when it fits
return s.includes('e') ? Number(s).toString() : s;
}
console.log((123456).toPrecision(3)); // "1.23e+5"
console.log(sigFigs(123456, 3)); // "123000"
console.log(sigFigs(0.00012345, 3)); // "0.000123"Range checking the precision argument
Unlike toFixed(), the valid range starts at 1 — zero significant digits is meaningless, so it throws rather than returning an empty string.
const n = 3.14159265;
console.log(n.toPrecision(1)); // "3"
console.log(n.toPrecision(9)); // "3.14159265"
console.log(n.toPrecision()); // "3.14159265" (same as toString)
try {
n.toPrecision(0);
} catch (err) {
console.log(err.name); // "RangeError"
}
Discussion