Number.prototype.toString()

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

Revision Content

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

Summary

Returns a string representing the specified Number object.

Syntax

numObj.toString(radix)

Parameter

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

Throws

{{jsxref("Global_Objects/RangeError", "RangeError")}}

If toString() is given a radix not between 2 and 36, a RangeError is thrown.

Description

The Number object overrides the toString() method of the {{jsxref("Global_Objects/Object", "Object")}} object; it does not inherit {{jsxref("Global_Objects/Object/toString", "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 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"

Specifications

Specification Status Comment
ECMAScript 1st Edition. Implemented in JavaScript 1.1 Standard Initial definition.
{{SpecName('ES6', 'sec-number.prototype.tostring', 'Number.prototype.tostring')}} {{Spec2('ES6')}}  

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/toFixed", "numObj.toFixed()")}}
  • {{jsxref("Global_Objects/Number/toPrecision", "numObj.toExponential()")}}
  • {{jsxref("Global_Objects/Number/toString", "numObj.toPrecision()")}}

Revision Source

<div>
  {{JSRef("Global_Objects", "Number")}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>Returns a string representing the specified Number object.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code><em>numObj</em>.toString(<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>
<h3 name="Description">Throws</h3>
<dl>
  <dt>
    {{jsxref("Global_Objects/RangeError", "RangeError")}}</dt>
  <dd>
    <p>If <code>toString()</code> is given a radix not between 2 and 36, a <code>RangeError</code> is thrown.</p>
  </dd>
</dl>
<h2 id="Description" name="Description">Description</h2>
<p>The <code>Number</code> object overrides the <code>toString()</code> method of the {{jsxref("Global_Objects/Object", "Object")}}<code> </code>object; it does not inherit {{jsxref("Global_Objects/Object/toString", "Object.toString()")}}. 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 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="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 1st Edition. Implemented in JavaScript 1.1</td>
      <td>Standard</td>
      <td>Initial definition.</td>
    </tr>
    <tr>
      <td>{{SpecName('ES6', 'sec-number.prototype.tostring', 'Number.prototype.tostring')}}</td>
      <td>{{Spec2('ES6')}}</td>
      <td>&nbsp;</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/toFixed", "numObj.toFixed()")}}</li>
  <li>{{jsxref("Global_Objects/Number/toPrecision", "numObj.toExponential()")}}</li>
  <li>{{jsxref("Global_Objects/Number/toString", "numObj.toPrecision()")}}</li>
</ul>
Revert to this revision