JS: Number.prototype.toPrecision

By Xah Lee. Date: . Last updated: .

number.toPrecision ( precision )

Return a string representation of number, with n significant digits.

Result may be in exponential notation. For example, "1e+1" meaning 1*10^1, or 10.

If precision is undefined, call ToString instead.

const num = 3.123;
console.log ( num.toPrecision ( 1 ) === "3" ); // true
console.log ( num.toPrecision ( 2 ) === "3.1" ); // true
console.log ( num.toPrecision ( 3 ) === "3.12" ); // true
console.log ( num.toPrecision ( 4 ) === "3.123" ); // true
console.log ( num.toPrecision ( 5 ) === "3.1230" ); // true
const num = 12.345;
console.log ( num.toPrecision ( 1 ) === "1e+1" ); // true
console.log ( num.toPrecision ( 2 ) === "12" ); // true
console.log ( num.toPrecision ( 3 ) === "12.3" ); // true
const num = 0.123;
console.log ( num.toPrecision ( 1 ) === "0.1" ); // true
console.log ( num.toPrecision ( 2 ) === "0.12" ); // true
console.log ( num.toPrecision ( 3 ) === "0.123" ); // true

JS Number

  1. Format Number
  2. String to Number
  3. Number Object
  4. Number.prototype
Liket it? I spend 2 years writing this tutorial. Help me spread it. Tell your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS in Depth
  4. JS Object Ref
  5. DOM Scripting
  6. SVG
  7. Blog