Number.prototype.toFixed()

  • Revision slug: JavaScript/Reference/Global_Objects/Number/toFixed
  • Revision title: toFixed
  • Revision id: 310617
  • Created:
  • Creator: speaktorob
  • Is current revision? No
  • Comment Corrected last toFixed example (15.0.1/OSX 10.8.1)

Revision Content

 

Summary

Formats a number using fixed-point notation

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

Syntax

number.toFixed( {{ mediawiki.external('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

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.
TypeError
If this method is invoked on an object that is not a Number.

Example

var n = 12345.6789;

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

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

n.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)

See Also

Number.toExponential(), Number.toLocaleString(), Number.toPrecision(), Number.toString(), Number.toSource(), Number.valueOf()

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

Revision Source

<p>&nbsp;</p>
<h3 id="Summary" name="Summary">Summary</h3>
<p>Formats a number using fixed-point notation</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en/JavaScript/Reference/Global_Objects/Number" title="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>
<h3 id="Syntax" name="Syntax">Syntax</h3>
<p><code><em>number</em>.toFixed( {{ mediawiki.external('<i>digits</i>') }} )</code></p>
<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>
    RangeError</dt>
  <dd>
    If digits 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>
    TypeError</dt>
  <dd>
    If this method is invoked on an object that is not a <code>Number</code>.</dd>
</dl>
<h3 id="Example" name="Example">Example</h3>
<pre>
var n = 12345.6789;

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

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

n.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>
<h3 id="See_Also" name="See_Also">See Also</h3>
<p><a href="/en/JavaScript/Reference/Global_Objects/Number/toExponential" title="en/JavaScript/Reference/Global_Objects/Number/toExponential">Number.toExponential()</a>, <a href="/en/JavaScript/Reference/Global_Objects/Number/ToLocaleString" title="en/JavaScript/Reference/Global_Objects/Number/ToLocaleString">Number.toLocaleString()</a>, <a href="/en/JavaScript/Reference/Global_Objects/Number/toPrecision" title="en/JavaScript/Reference/Global_Objects/Number/toPrecision">Number.toPrecision()</a>, <a href="/en/JavaScript/Reference/Global_Objects/Number/toString" title="en/JavaScript/Reference/Global_Objects/Number/toString">Number.toString()</a>, <a href="/en/JavaScript/Reference/Global_Objects/Number/toSource" title="en/JavaScript/Reference/Global_Objects/Number/toSource">Number.toSource()</a>, <a href="/en/JavaScript/Reference/Global_Objects/Number/valueOf" title="en/JavaScript/Reference/Global_Objects/Number/valueOf">Number.valueOf()</a></p>
<p>{{ languages( { "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Number/toFixed", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Number/toFixed" } ) }}</p>
Revert to this revision