Number.prototype.toPrecision()

toPrecision(precision)
Returns: string · Added in vES1 · Updated March 13, 2026 · Number
number precision formatting

The toPrecision() method returns a string representing the number to the specified precision. Unlike toFixed(), which specifies digits after the decimal point, toPrecision() controls the total number of significant digits — making it ideal for scientific data where precision matters across the entire number.

Syntax

num.toPrecision(precision)

Parameters

ParameterTypeDescription
precisionnumberNumber of significant digits (1-21). If omitted, behaves like toString().

Return Value

Returns a string (not a number). This is a key distinction from toFixed() and toExponential().

How It Differs from toFixed()

The critical difference: toFixed() counts digits after the decimal point, while toPrecision() counts total significant digits across the entire number.

const num = 123.456;

// toFixed() - digits AFTER decimal point
num.toFixed(2);    // "123.46"
num.toFixed(4);    // "123.4560"

// toPrecision() - TOTAL significant digits
num.toPrecision(4);  // "123.5" (4 significant digits)
num.toPrecision(6);  // "123.456" (6 significant digits)

Notice how toPrecision(4) produces “123.5” because it rounds to 4 total significant digits, not 4 decimal places. This makes toPrecision() more intuitive for scientific measurements where you care about relative precision, not absolute decimal positions.

Behavior When Precision Is Omitted

When called without arguments, toPrecision() behaves identically to toString() — returning the full number without any rounding:

const num = 123.456;

num.toPrecision();   // "123.456"
num.toString();     // "123.456"
// They return the same result!

This is useful when you want flexible precision — pass a parameter when you need rounding, omit it for full precision.

Scientific Data Formatting

toPrecision() excels at formatting scientific data because it automatically switches to scientific notation for very large or very small numbers:

// Small numbers - scientific notation kicks in automatically
0.000012345.toPrecision(3);   // "1.23e-5"
0.000012345.toPrecision(5);   // "1.2345e-5"

// Large numbers
1234567.toPrecision(3);       // "1.23e+6"
1234567.toPrecision(6);        // "1.235e+6"

This automatic switching makes it perfect for displaying measurements, lab results, and astronomical or atomic-scale values where precision varies across magnitudes.

Examples

Basic Usage with Various Precision Levels

const num = 5.12345;

num.toPrecision(1);   // "5"
num.toPrecision(2);   // "5.1"
num.toPrecision(3);   // "5.12"
num.toPrecision(5);   // "5.1235"
num.toPrecision(8);   // "5.1234500"

Comparison with toFixed()

const value = 99.957;

console.log(value.toFixed(2));      // "99.96"  (2 decimal places)
console.log(value.toPrecision(4));  // "99.96"  (4 significant digits)

const small = 0.00512;

console.log(small.toFixed(3));       // "0.005"  (3 decimal places)
console.log(small.toPrecision(3));   // "0.00512" (3 significant digits!)

Formatting Scientific Measurements

function formatScientific(value, sigFigs = 4) {
  const formatted = value.toPrecision(sigFigs);
  
  // Check if the original value was precisely represented
  const wasRounded = Number(formatted) !== value;
  
  return wasRounded ? `${formatted} (approx)` : formatted;
}

// Various scientific values
formatScientific(299792458);      // "2.998e+8"
formatScientific(6.626e-34);      // "6.626e-34"
formatScientific(1.60217663e-19); // "1.602e-19 (approx)"
formatScientific(9.10938e-31);    // "9.109e-31 (approx)"

Edge Cases

(0).toPrecision(2);         // "0.0"
(100).toPrecision(1);       // "1e+2"
(NaN).toPrecision(2);       // "NaN"
(1).toPrecision(21);        // RangeError: precision must be between 1 and 21
(1).toPrecision(0);         // RangeError: precision must be between 1 and 21

See Also