API · JavaScript
JavaScript Number toPrecision() Method
This article introduces the number.toPrecision() method that is used to format the number object to a particular precision.
On this page
The Number.prototype.toPrecision() method formats a number to a chosen count of significant digits and returns the formatted value as a string. Significant digits count from the first nonzero digit; they are not simply the digits after the decimal point. This makes the method useful when a display needs a consistent amount of precision, while calculations should continue to use the original numeric value.
What toPrecision() Does
toPrecision() chooses a decimal representation with the requested number of significant digits. It may round the final significant digit, add trailing zeros to reach the requested precision, or use exponential notation when that is the suitable representation. The method does not change the number it is called on; it creates a string for display or serialization.
Run this example:
const amount = 123.456;
console.log(amount.toPrecision(4));
123.5
The call asks for four significant digits. Its returned value is a string, so use the original amount for later numeric calculations.
Syntax, Parameter, and Return Value
Call the method on a Number value as number.toPrecision() or number.toPrecision(precision). The optional precision argument is an integer from 1 through 100, inclusive. When it is omitted, JavaScript uses the number’s ordinary string representation rather than padding it to a chosen count. Passing an argument selects the total significant-digit count, including digits on both sides of the decimal point.
The return value is always a string. For example, typeof (12.3).toPrecision(3) is "string". If another calculation needs a number, keep the original number or convert the returned string explicitly; do not assume formatted text remains numeric.
Format Numbers with toPrecision()
The following runnable example demonstrates rounding, trailing zeros, exponential notation, and the omitted-argument behavior. Each call formats a valid numeric value; the exponent in 123.34e5 is part of a valid JavaScript numeric literal.
const first = 1987.132432;
const second = 2.112;
const large = 123.34e5;
console.log(first.toPrecision(4));
console.log(second.toPrecision(6));
console.log(large.toPrecision(2));
console.log(first.toPrecision());
1987
2.11200
1.2e+7
1987.132432
With four significant digits, the first value is rounded and needs no fractional digits in its representation. The second call pads with trailing zeros to show six significant digits. A large value with only two significant digits is compactly represented using exponential notation. Omitting the argument produces the ordinary number string. These are strings; avoid using their formatting as a substitute for numeric rounding in calculations.
Handle Invalid Precision Values
JavaScript converts the argument to an integer by truncating a finite fractional value toward zero, then checks whether the result is from 1 to 100, inclusive. A fractional argument does not necessarily cause a RangeError: for example, 3.7 is treated as 3, while 0.9 becomes 0 and is outside the allowed range. A converted value below 1 or above 100 causes a RangeError; the method does not return a special error value. Catch the exception if the precision comes from user input, then validate or report it before formatting.
const value = 1987.132432;
const precision = 0;
try {
console.log(value.toPrecision(precision));
} catch (error) {
console.log(error.name);
}
RangeError
The handler captures the error name so the program can continue. A production interface can instead check Number.isInteger(precision) and the inclusive range before calling the method, then show a useful message or choose a documented default.
Convert Values Before Formatting
toPrecision() is a Number method. A string such as "12.345" does not have this method, so calling it directly produces a TypeError because the property is not callable. Convert text to a number first, and validate the conversion: Number(text) returns NaN when the text is not a valid numeric representation, and NaN.toPrecision() is not a useful formatted result.
const text = "12.345";
const value = Number(text);
if (Number.isNaN(value)) {
console.log("Enter a valid number.");
} else {
console.log(value.toPrecision(4));
}
12.35
For a value that is already numeric, call toPrecision() directly. For data read from a form, parse or convert it and check the result before formatting. This keeps type errors separate from precision-range errors and lets the caller explain which input needs correction.