mozilla

Revision 445645 of Number.prototype.toString()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Number/toString
  • Revision title: Number.toString
  • Revision id: 445645
  • Created:
  • Creator: kscarfone
  • Is current revision? No
  • Comment Editorial review

Revision Content

Summary

Returns a string representing the specified Number object.

Method of Number
Implemented in JavaScript 1.1
ECMAScript Edition ECMAScript 1st Edition

Syntax

number.toString( {{mediawiki.external('radix')}} )

Parameter

radix
An integer between 2 and 36 specifying the base to use for representing numeric values.

Description

The Number object overrides the toString method of the Object object; it does not inherit Object.toString. For Number objects, the toString method returns a string representation of the object in the specified radix.

The toString method parses its first argument, and attempts to return a string representation in the specified radix (base). For radixes above 10, the letters of the alphabet indicate numerals greater than 9. For example, for hexadecimal numbers (base 16), a through f are used.

If toString is given a radix not between 2 and 36, an exception is thrown.

If the radix is not specified, the preferred radix is assumed to be 10.

If the number is negative, the sign is preserved. Especially if the radix is 2, it's returning the binary (zeros and ones) of the number preceeded by a - sign but the two's complement.

Examples

var count = 10;

print( count.toString() );   // displays "10"
print( (17).toString() );    // displays "17"

var x = 6;

print( x.toString(2) );      // displays "110"
print( (254).toString(16) ); // displays "fe"


print( (-10).toString(2) );  // displays "-1010"
print( (-0xff).toString(2) );// displays "-11111111"

See also

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>Returns a string representing the specified Number object.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Number" title="JavaScript/Reference/Global_Objects/Number"><code>Number</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.1</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 1st Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code><em>number</em>.toString( {{mediawiki.external('<i>radix</i>')}} )</code></pre>
<h3 id="Parameter" name="Parameter">Parameter</h3>
<dl>
  <dt>
    radix</dt>
  <dd>
    An integer between 2 and 36 specifying the base to use for representing numeric values.</dd>
</dl>
<h2 id="Description" name="Description">Description</h2>
<p>The <code>Number</code> object overrides the <code>toString</code> method of the <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object" title="JavaScript/Reference/Global_Objects/Object"><code>Object</code></a> object; it does not inherit <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/toString" title="JavaScript/Reference/Global_Objects/Object/toString"><code>Object.toString</code></a>. For <code>Number</code> objects, the <code>toString</code> method returns a string representation of the object in the specified radix.</p>
<p>The <code>toString</code> method parses its first argument, and attempts to return a string representation in the specified radix (base). For radixes above 10, the letters of the alphabet indicate numerals greater than 9. For example, for hexadecimal numbers (base 16), <code>a</code> through <code>f</code> are used.</p>
<p>If <code>toString</code> is given a radix not between 2 and 36, an exception is thrown.</p>
<p>If the radix is not specified, the preferred radix is assumed to be 10.</p>
<p>If the <code>number</code> is negative, the sign is preserved. Especially if the radix is 2, it's returning the binary (zeros and ones) of the <code>number</code> preceeded by a <code>-</code> sign but the two's complement.</p>
<h2 id="Examples" name="Examples">Examples</h2>
<pre class="brush:js">
var count = 10;

print( count.toString() );   // displays "10"
print( (17).toString() );    // displays "17"

var x = 6;

print( x.toString(2) );      // displays "110"
print( (254).toString(16) ); // displays "fe"


print( (-10).toString(2) ); &nbsp;// displays "-1010"
print( (-0xff).toString(2) );// displays "-11111111"
</pre>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Number/toExponential" title="JavaScript/Reference/Global_Objects/Number/toExponential">Number.prototype.toExponential</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Number/ToLocaleString" title="JavaScript/Reference/Global_Objects/Number/ToLocaleString">Number.prototype.toLocaleString</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Number/toPrecision" title="JavaScript/Reference/Global_Objects/Number/toPrecision">Number.prototype.toPrecision</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Number/toFixed" title="JavaScript/Reference/Global_Objects/Number/toFixed">Number.prototype.toFixed</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Number/toSource" title="JavaScript/Reference/Global_Objects/Number/toSource">Number.prototype.toSource</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Number/valueOf" title="JavaScript/Reference/Global_Objects/Number/valueOf">Number.prototype.valueOf</a></li>
</ul>
Revert to this revision