JavaScript Number toPrecision() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Significant digits

What You’ll Learn

Number.prototype.toPrecision() formats a number to a chosen count of significant digits (same API as MDN Number.toPrecision). It may use plain or exponential form. This tutorial covers syntax, when expo appears, comparisons, five examples, and try-it labs.

01

Syntax

toPrecision(p)

02

Returns

string

03

Precision

1 to 100

04

Default

like toString

05

Form

plain or e±

06

Baseline

Everywhere

Introduction

Significant digits measure how many meaningful figures you keep from the start of a number — not how many places sit after the decimal point. That is why (5.123456).toPrecision(2) is "5.1", while (1234.5).toPrecision(2) becomes "1.2e+3".

Use toPrecision() when you care about overall precision (science, sensors, rounded reports) more than a fixed UI decimal width.

This page is part of JavaScript Number Methods. Related formatters include toFixed(), toExponential(), and toString().

Understanding the toPrecision() Method

Call number.toPrecision() or number.toPrecision(precision) on a number primitive or Number object. You always get a string; the number itself does not change.

Omit precision and the result matches toString(). Pass a value from 1 to 100 to round to that many significant digits. Depending on the magnitude, the engine may print a plain string or switch to exponential notation.

💡
Beginner Tip

Think “how many digits of meaning,” not “how many decimals.” For fixed decimals like prices, prefer toFixed(). Use parentheses for integers: (1234).toPrecision(3).

📝 Syntax

General form of Number.prototype.toPrecision:

JavaScript
number.toPrecision()
number.toPrecision(precision)

Parameters

  • precision (number, optional) — integer from 1 to 100. Count of significant digits. If omitted, behaves like toString().

Return value

Always a string. Special cases:

ValueReturns (example)
Omit precision(5.123456).toPrecision()"5.123456" (like toString())
5 significant digits(5.123456).toPrecision(5)"5.1235"
2 significant digits(5.123456).toPrecision(2)"5.1"
Switches to expo(1234.5).toPrecision(2)"1.2e+3"
Tiny value, padded(0.000123).toPrecision(5)"0.00012300"
Infinity / NaN"Infinity" / "NaN"

Exceptions

  • RangeErrorprecision < 1 or > 100.
  • TypeError — called on a non-Number this value (for example a plain object).

Common patterns

JavaScript
(5.123456).toPrecision();     // "5.123456"
(5.123456).toPrecision(5);    // "5.1235"
(5.123456).toPrecision(2);    // "5.1"
(1234.5).toPrecision(2);      // "1.2e+3"
(123.456).toPrecision(4);     // "123.5"

⚡ Quick Reference

GoalCode
Same as toString()n.toPrecision()
3 significant digitsn.toPrecision(3)
Force scientific formn.toExponential(2)
Fixed decimalsn.toFixed(2)
Science-style helperx.toPrecision(4)
Parse then formatNumber(x).toPrecision(4)

🔍 At a Glance

Four facts to remember about Number.prototype.toPrecision().

Returns
string

The number is never changed

Precision
1–100

Significant digits (not decimals)

Form
auto

Plain or exponential by magnitude

Support
Baseline

Works in all modern engines

📋 toPrecision() vs toFixed() vs toExponential()

toPrecision(p)toFixed(d)toExponential(d)
Main jobSignificant digitsFixed decimal placesAlways scientific form
Example for 1234.5"1.2e+3" (p=2)"1234.50" (d=2)"1.23e+3" (d=2)
Best forScience / overall precisionUI / pricesForced expo display

Examples Gallery

Examples follow MDN patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

Default call, then shrink significant digits.

Example 1 — Default (like toString)

Omit the argument to get the same kind of string as toString().

JavaScript
(5.123456).toPrecision();  // "5.123456"
Try It Yourself

How It Works

With no precision, the engine does not round to a digit budget — it prints a normal distinguishing string, just like toString().

Example 2 — Five Significant Digits

Pass 5 to keep five meaningful figures.

JavaScript
(5.123456).toPrecision(5);  // "5.1235"
Try It Yourself

How It Works

The sixth digit would be 4, so the fifth fractional place rounds from 4 up to 5 in the result "5.1235".

📈 Practical Patterns

Shorter precision, exponential switch, and an MDN-style helper.

Example 3 — Two Significant Digits

A tight digit budget rounds aggressively while staying in plain form here.

JavaScript
(5.123456).toPrecision(2);  // "5.1"
Try It Yourself

How It Works

Only two significant digits are kept: 5 and 1. Compare with toFixed(2), which would target two places after the decimal instead.

Example 4 — Switches to Exponential

When the exponent is large relative to precision, scientific form appears.

JavaScript
(1234.5).toPrecision(2);  // "1.2e+3"
Try It Yourself

How It Works

With precision 2, writing 1200 as plain digits would need more figures than allowed, so the engine prints "1.2e+3". Try (6) to get "1234.50".

Example 5 — Four-Digit Helper

An MDN-style helper that always asks for four significant digits.

JavaScript
(123.456).toPrecision(4);  // "123.5"
Try It Yourself

How It Works

Four significant digits round 123.456 to "123.5". The same helper on 1.23e5 would yield "1.230e+5".

🚀 Common Use Cases

  • Science & engineering — report measurements with a fixed figure count.
  • Sensor / telemetry UIs — keep noisy floats to a readable precision.
  • Reports & exports — consistent significant-digit text across scales.
  • Teaching precision — demonstrate significant figures live in the browser.
  • Adaptive display — let the engine pick plain vs expo for the magnitude.
  • Not fixed money columns — prefer toFixed(2) or Intl.NumberFormat for prices.

🧠 How toPrecision() Builds the String

1

Read the number

Must be a Number primitive or wrapper.

Input
2

Choose precision

Omit for toString-like output, or pick 1–100.

Digits
3

Pick the form

Plain string or exponential, based on magnitude.

Shape
4

Return string

Form like "5.1" or "1.2e+3"; number unchanged.

📝 Notes

  • Does not change the number — returns a new string.
  • precision must be 1–100 (unlike toFixed, which allows 0).
  • Omitting precision matches toString() behavior.
  • Exponential form appears when the exponent ≥ precision or < -6.
  • Floating-point limits still apply — very high precision can show binary noise.
  • For locale-aware formatting, use toLocaleString() / Intl.NumberFormat.

Browser & Runtime Support

Number.prototype.toPrecision() is Baseline Widely available — part of JavaScript since early ECMAScript editions and supported in every modern browser and Node.js.

Baseline · Widely available

Number.prototype.toPrecision()

Safe for production everywhere. Optional precision 1–100 works consistently across engines.

100% Universal 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
Number.toPrecision() Excellent

Bottom line: Use freely for significant-digit display. Prefer toFixed or Intl for fixed currency columns.

Conclusion

Number.prototype.toPrecision() turns any number into a string with a chosen significant-digit budget — sometimes plain, sometimes exponential. It shines when overall precision matters more than a fixed decimal width.

Continue with Number.isFinite() for safe finite checks, toFixed() for fixed decimals, or return to the Number methods hub.

💡 Best Practices

✅ Do

  • Use parentheses for integer literals: (1234).toPrecision(3)
  • Pick precision between 1 and 100
  • Expect either plain or e+/e- strings
  • Prefer toFixed when you need exact decimal places
  • Prefer toExponential when you always want scientific form

❌ Don’t

  • Write 10.toPrecision(2) without space/parens (syntax error)
  • Pass precision 0 (throws RangeError)
  • Confuse significant digits with decimal places
  • Assume the result stays in plain form for large values
  • Use it as bank-grade money formatting

Key Takeaways

Knowledge Unlocked

Five things to remember about Number.toPrecision()

Format significant digits — plain or exponential as needed.

5
Core concepts
🔢 02

Range

1–100

Param
Σ 03

Counts

sig figs

Meaning
e 04

Form

auto

Shape
05

Safe

Baseline

Support

❓ 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.
Did you know?

Leading zeros after a decimal point do not count as significant digits. That is why (0.000123).toPrecision(2) is "0.00012" — the meaningful figures start at 1 and 2.

More Number Methods

Return to the hub for the growing Number.prototype collection.

Number methods hub →

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.

8 people found this page helpful