Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 481317 of Number.prototype.toFixed()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Number/toFixed
  • Revision title: numObj.toFixed()
  • Revision id: 481317
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment

Revision Content

{{JSRef("Global_Objects", "Number")}}

Summary

Formats a number using fixed-point notation.

Syntax

numObj.toFixed(digits)

Parameter

digits
The number of digits to appear after the decimal point; this may be a value between 0 and 20, inclusive, and implementations may optionally support a larger range of values. If this argument is omitted, it is treated as 0.

Returns

A string representation of number that does not use exponential notation and has exactly digits digits after the decimal place. The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length. If number is greater than 1e+21, this method simply calls Number.toString() and returns a string in exponential notation.

Throws

{{jsxref("Global_Objects/RangeError", "RangeError")}}
If digits is too small or too large. Values between 0 and 20, inclusive, will not cause a RangeError. Implementations are allowed to support larger and smaller values as well.
{{jsxref("Global_Objects/TypeError", "TypeError")}}
If this method is invoked on an object that is not a Number.

Examples

var numObj = 12345.6789;

numObj.toFixed();         // Returns "12346": note rounding, no fractional part

numObj.toFixed(1);        // Returns "12345.7": note rounding

numObj.toFixed(6);        // Returns "12345.678900": note added zeros

(1.23e+20).toFixed(2);    // Returns "123000000000000000000.00"

(1.23e-10).toFixed(2);    // Returns "0.00"

2.34.toFixed(1);          // Returns "2.3"

-2.34.toFixed(1);         // Returns -2.3 (due to operator precedence, negative numbers don't return a string...)

(-2.34).toFixed(1);       // Returns "-2.3" (...unless you use parentheses)

Specifications

Specification Status Comment
ECMAScript 3rd Edition. Implemented in JavaScript 1.5 Standard Initial definition.
{{SpecName('ES6', '#sec-number.prototype.tofixed', 'Number.prototype.toFixed')}} {{Spec2('ES6')}} No change since ES 3rd Edition.

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}

See Also

  • {{jsxref("Global_Objects/Number/toExponential", "numObj.toExponential()")}}
  • {{jsxref("Global_Objects/Number/toPrecision", "numObj.toPrecision()")}}
  • {{jsxref("Global_Objects/Number/toString", "numObj.toString()")}}

Revision Source

<div>
  {{JSRef("Global_Objects", "Number")}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>Formats a number using fixed-point notation.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code><em>numObj</em>.toFixed(<i>digits</i>)</code></pre>
<h3 id="Parameter" name="Parameter">Parameter</h3>
<dl>
  <dt>
    digits</dt>
  <dd>
    The number of digits to appear after the decimal point; this may be a value between 0 and 20, inclusive, and implementations may optionally support a larger range of values. If this argument is omitted, it is treated as 0.</dd>
</dl>
<h3 id="Returns" name="Returns">Returns</h3>
<p>A string representation of <code>number</code> that does not use exponential notation and has exactly <code>digits</code> digits after the decimal place. The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length. If <code>number</code> is greater than 1e+21, this method simply calls <code>Number.toString()</code> and returns a string in exponential notation.</p>
<h3 id="Throws" name="Throws">Throws</h3>
<dl>
  <dt>
    {{jsxref("Global_Objects/RangeError", "RangeError")}}</dt>
  <dd>
    If <em>digits</em> is too small or too large. Values between 0 and 20, inclusive, will not cause a <code>RangeError</code>. Implementations are allowed to support larger and smaller values as well.</dd>
</dl>
<dl>
  <dt>
    {{jsxref("Global_Objects/TypeError", "TypeError")}}</dt>
  <dd>
    If this method is invoked on an object that is not a <code>Number</code>.</dd>
</dl>
<h2 id="Example" name="Example">Examples</h2>
<pre class="brush: js">
var numObj = 12345.6789;

numObj.toFixed();         // Returns "12346": note rounding, no fractional part

numObj.toFixed(1);        // Returns "12345.7": note rounding

numObj.toFixed(6);        // Returns "12345.678900": note added zeros

(1.23e+20).toFixed(2);    // Returns "123000000000000000000.00"

(1.23e-10).toFixed(2);    // Returns "0.00"

2.34.toFixed(1);          // Returns "2.3"

-2.34.toFixed(1);         // Returns -2.3 (due to operator precedence, negative numbers don't return a string...)

(-2.34).toFixed(1);       // Returns "-2.3" (...unless you use parentheses)

</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
  <tbody>
    <tr>
      <th scope="col">Specification</th>
      <th scope="col">Status</th>
      <th scope="col">Comment</th>
    </tr>
    <tr>
      <td>ECMAScript 3rd Edition. Implemented in JavaScript 1.5</td>
      <td>Standard</td>
      <td>Initial definition.</td>
    </tr>
    <tr>
      <td>{{SpecName('ES6', '#sec-number.prototype.tofixed', 'Number.prototype.toFixed')}}</td>
      <td>{{Spec2('ES6')}}</td>
      <td>No change since ES 3rd Edition.</td>
    </tr>
  </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<div>
  {{CompatibilityTable}}</div>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari (WebKit)</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Phone</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="See_Also" name="See_Also">See Also</h2>
<ul>
  <li>{{jsxref("Global_Objects/Number/toExponential", "numObj.toExponential()")}}</li>
  <li>{{jsxref("Global_Objects/Number/toPrecision", "numObj.toPrecision()")}}</li>
  <li>{{jsxref("Global_Objects/Number/toString", "numObj.toString()")}}</li>
</ul>
Revert to this revision