Example 1 — Significant Digits
Round to a chosen count of significant digits; omit precision for full default string.
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" How It Works
- Omit precision. Behaves like
toString()for a normal finite number. - Five digits. Rounds
5.123456to five significant digits →"5.1235". - Two digits. Keeps only
5and1→"5.1". - String result.
typeofis"string"— parse again before more math.

