Number.prototype.toPrecision()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Number/toPrecision
  • Revision title: toPrecision
  • Revision id: 422423
  • Created:
  • Creator: evilpie
  • Is current revision? No
  • Comment

Revision Content

 

Summary

Returns a string representing the Number object to the specified precision.

Method of Number
Implemented in JavaScript 1.5
ECMAScript Edition ECMAScript 3rd Edition

Syntax

number.toPrecision( [ precision ] )

Parameter

precision
An integer specifying the number of significant digits.

Returns

A string representing a Number object in fixed-point or exponential notation rounded to precision significant digits. See the discussion of rounding in the description of the toFixed method, which also applies to toPrecision.

If the precision argument is omitted, behaves as Number.toString. If it is a non-integer value, it is rounded to the nearest integer. After rounding, if that value is not between 1 and 100 (inclusive), a RangeError is thrown.

ECMA-262 only requires a precision of up to 21 significant digits. Other implementations may not support precisions higher than required by the standard.

Example

var num = 5.123456;
console.log("num.toPrecision() is " + num.toPrecision());   //displays 5.123456
console.log("num.toPrecision(5) is " + num.toPrecision(5)); //displays 5.1235
console.log("num.toPrecision(2) is " + num.toPrecision(2)); //displays 5.1
console.log("num.toPrecision(1) is " + num.toPrecision(1)); //displays 5

// note that scientific notation might be returned in some circumstances
console.log((1234.5).toPrecision(2)); // "1.2e+3"

See Also

{{ languages( { "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Number/toPrecision", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Number/toPrecision" } ) }}

Revision Source

<p>&nbsp;</p>
<h2 id="Summary" name="Summary">Summary</h2>
<p>Returns a string representing the Number object to the specified precision.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en/JavaScript/Reference/Global_Objects/Number"><code>Number</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.5</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 3rd Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<p><code><em>number</em>.toPrecision( [ <em>precision</em> ] )</code></p>
<h3 id="Parameter" name="Parameter">Parameter</h3>
<dl>
  <dt>
    precision</dt>
  <dd>
    An integer specifying the number of significant digits.</dd>
</dl>
<h3 id="Returns" name="Returns">Returns</h3>
<p>A string representing a Number object in fixed-point or exponential notation rounded to <code>precision</code> significant digits. See the discussion of rounding in the description of the <a href="/en/JavaScript/Reference/Global_Objects/Number/toFixed" title="en/Core_JavaScript_1.5_Reference/Global_Objects/Number/toFixed">toFixed</a> method, which also applies to <code>toPrecision</code>.</p>
<p>If the <code>precision</code> argument is omitted, behaves as <a href="/en/JavaScript/Reference/Global_Objects/Number/toString" title="en/Core_JavaScript_1.5_Reference/Global_Objects/Number/toString">Number.toString</a>. If it is a non-integer value, it is rounded to the nearest integer. After rounding, if that value is not between 1 and 100 (inclusive), a <a href="/En/Core_JavaScript_1.5_Reference/Global_Objects/RangeError" title="en/Core_JavaScript_1.5_Reference/Global_Objects/RangeError">RangeError</a> is thrown.</p>
<div class="note">
  <a href="/en/ECMAScript" title="en/ECMAScript">ECMA-262</a> only requires a precision of up to 21 significant digits. Other implementations may not support precisions higher than required by the standard.</div>
<h2 id="Example" name="Example">Example</h2>
<pre class="eval">
var num = 5.123456;
console.log("num.toPrecision() is " + num.toPrecision());   //displays 5.123456
console.log("num.toPrecision(5) is " + num.toPrecision(5)); //displays 5.1235
console.log("num.toPrecision(2) is " + num.toPrecision(2)); //displays 5.1
console.log("num.toPrecision(1) is " + num.toPrecision(1)); //displays 5

// note that scientific notation might be returned in some circumstances
console.log((1234.5).toPrecision(2)); // "1.2e+3"
</pre>
<h2 id="See_Also" name="See_Also">See Also</h2>
<ul>
  <li><a href="/en/JavaScript/Reference/Global_Objects/Number/toExponential" title="en/Core_JavaScript_1.5_Reference/Global_Objects/Number/toExponential">toExponential</a></li>
  <li><a href="/en/JavaScript/Reference/Global_Objects/Number/toFixed" title="en/Core_JavaScript_1.5_Reference/Global_Objects/Number/toFixed">toFixed</a></li>
  <li><a href="/en/JavaScript/Reference/Global_Objects/Number/toString" title="en/Core_JavaScript_1.5_Reference/Global_Objects/Number/toString">toString</a></li>
</ul>
<p>{{ languages( { "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Number/toPrecision", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Number/toPrecision" } ) }}</p>
Revert to this revision