JavaScript Number toPrecision() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

number.toPrecision(precision) returns a string of the number rounded to a chosen count of significant digits (1–100). It may use plain or scientific form. Omit precision and it behaves like toString().

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call number.toPrecision. Start with this line.

JavaScript
n.toPrecision() / n.toPrecision(precision)
CallWhat it does
n.toPrecision() / n.toPrecision(precision)Primary signature.
number.toPrecision(...)Call with the arguments your case needs.

Built into JavaScript. Call toPrecision(...) on a number (or Number instance) — no import required.

Library declarations
JavaScript
n.toPrecision() / n.toPrecision(precision)

Parameters

ParameterDescription
precisionOptional. Significant digits (1–100)

Return Value

Return ValueDescription
*Always a string

JavaScript Examples

Example 1 — Significant Digits

Round to a chosen count of significant digits; omit precision for full default string.

JavaScript
console.log((5.123456).toPrecision());   // "5.123456"
console.log((5.123456).toPrecision(5));  // "5.1235"
console.log((5.123456).toPrecision(2));  // "5.1"
console.log(typeof (5.123456).toPrecision(2)); // "string"
Try It Yourself

How It Works

  1. Omit precision. Behaves like toString() for a normal finite number.
  2. Five digits. Rounds 5.123456 to five significant digits → "5.1235".
  3. Two digits. Keeps only 5 and 1 → "5.1".
  4. String result. typeof is "string" — parse again before more math.

Example 2 — Switches to Exponential

Large (or tiny) values often use scientific form for a small precision.

JavaScript
console.log((1234.5).toPrecision(2));     // "1.2e+3"
console.log((123.456).toPrecision(4));    // "123.5"
console.log((0.000123).toPrecision(2));   // "0.00012"
console.log((0.000000123).toPrecision(2)); // "1.2e-7"
Try It Yourself

How It Works

  1. Large + low precision. 1234.5 with 2 digits → scientific "1.2e+3".
  2. Still plain. 123.456 with 4 digits fits plain form → "123.5".
  3. Leading zeros. Zeros before the first non-zero digit are not significant.
  4. Very small. Exponent < −6 triggers scientific form ("1.2e-7").

Example 3 — vs toFixed and toExponential

Same input, three different formatting jobs.

JavaScript
var n = 1234.5;
console.log(n.toPrecision(2));    // "1.2e+3"
console.log(n.toFixed(2));        // "1234.50"
console.log(n.toExponential(2));  // "1.23e+3"
Try It Yourself

How It Works

  1. toPrecision. Counts significant digits; may switch to e form.
  2. toFixed. Counts digits after the decimal; stays plain.
  3. toExponential. Always scientific; the argument is fraction digits after the mantissa.
  4. Pick by intent. Significant figures → toPrecision; money/UI decimals → toFixed.

Browser & Runtime Support

Number.prototype.toPrecision() is part of early ECMAScript and is Baseline Widely available in modern browsers and Node.js.

✓ Baseline · Long-standing

Number.toPrecision()

Supported in Chrome, Firefox, Safari, Edge, and Node.js for years. Safe for modern apps without a polyfill.

98% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
toPrecision Excellent

Bottom line: Use toPrecision freely in new code for significant-digit formatting.

Frequently Asked Questions

Number.prototype.toPrecision() returns a string of the number rounded to a chosen count of significant digits. Example: (5.123456).toPrecision(2) is "5.1".
precision is an optional integer from 1 to 100 for how many significant digits to keep. If you omit it, the method behaves like toString(). Values outside 1–100 throw a RangeError.
When the exponent is greater than or equal to precision, or less than -6, the string uses scientific form (for example (1234.5).toPrecision(2) is "1.2e+3"). Otherwise it stays in plain fixed-looking form.
toPrecision() counts significant digits and may pick plain or exponential form. toFixed() always aims for a fixed count of digits after the decimal in plain form. toExponential() always uses scientific notation.
Always a string. Convert again with Number() or parseFloat if you need to continue doing math.
It is part of early ECMAScript and is Baseline Widely available — safe in every modern browser and Node.js.

Next: isFinite()

Learn how to check for finite numbers without coercion.

isFinite() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful