mozilla

Revision 78039 of parseInt()

  • Revision slug: JavaScript/Reference/Global_Objects/parseInt
  • Revision title: parseInt
  • Revision id: 78039
  • Created:
  • Creator: Adamite
  • Is current revision? No
  • Comment /* Parameters */
Tags: 

Revision Content

Summary

Core Function

Parses a string argument and returns an integer of the specified radix or base.

Syntax

parseInt(string{{mediawiki.external(', <i>radix</i>')}})

Parameters

string 
A string that represents the value you want to parse.
radix 
An integer that represents the radix of the above mentioned string.

Description

parseInt is a top-level function and is not associated with any object.

The parseInt function parses its first argument, a string, and attempts to return an integer of the specified radix (base). For example, a radix of 10 indicates to convert to a decimal number, 8 octal, 16 hexadecimal, and so on. 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 parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed.

If the radix is not specified or is specified as 0, JavaScript assumes the following:

  • If the input string begins with "0x", the radix is 16 (hexadecimal).
  • If the input string begins with "0", the radix is eight (octal). This feature is deprecated.
  • If the input string begins with any other value, the radix is 10 (decimal).

If the first character cannot be converted to a number, parseInt returns NaN.

For arithmetic purposes, the NaN value is not a number in any radix. You can call the isNaN function to determine if the result of parseInt is NaN. If NaN is passed on to arithmetic operations, the operation results will also be NaN.

Examples

Example: Using parseInt

The following examples all return 15:

  • parseInt("F", 16);
  • parseInt("17", 8);
  • parseInt("15", 10);
  • parseInt(15.99, 10);
  • parseInt("FXX123", 16);
  • parseInt("1111", 2);
  • parseInt("15*3", 10);
  • parseInt("12", 13);

The following examples all return NaN:

  • parseInt("Hello", 8); // Not a number at all
  • parseInt("0x7", 10); // Not in base 10 format
  • parseInt("546", 2); // Digits are not valid for binary representations

Even though the radix is specified differently, the following examples all return 17 because the input string begins with "0x".

  • parseInt("0x11", 16);
  • parseInt("0x11", 0);
  • parseInt("0x11");

See also

isNaN, parseFloat, Object.valueOf

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p><b>Core Function</b>
</p><p>Parses a string argument and returns an integer of the specified radix or base.
</p>
<h3 name="Syntax"> Syntax </h3>
<p><code>
parseInt(<i>string</i>{{mediawiki.external(', &lt;i&gt;radix&lt;/i&gt;')}})
</code>
</p>
<h3 name="Parameters"> Parameters </h3>
<dl><dt> <code>string</code> </dt><dd> A string that represents the value you want to parse.
</dd></dl>
<dl><dt> <code>radix</code> </dt><dd> An integer that represents the radix of the above mentioned string.
</dd></dl>
<h3 name="Description"> Description </h3>
<p><code>parseInt</code> is a top-level function and is not associated with any object.
</p><p>The <code>parseInt</code> function parses its first argument, a string, and attempts to return an integer of the specified radix (base). For example, a radix of 10 indicates to convert to a decimal number, 8 octal, 16 hexadecimal, and so on. 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.
</p><p>If <code>parseInt</code> encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. <code>parseInt</code> truncates numbers to integer values. Leading and trailing spaces are allowed.
</p><p>If the radix is not specified or is specified as 0, JavaScript assumes the following:
</p>
<ul><li> If the input <code>string</code> begins with "0x", the radix is 16 (hexadecimal).
</li></ul>
<ul><li> If the input <code>string</code> begins with "0", the radix is eight (octal). This feature is deprecated.
</li></ul>
<ul><li> If the input <code>string</code> begins with any other value, the radix is 10 (decimal).
</li></ul>
<p>If the first character cannot be converted to a number, <code>parseInt</code> returns <code>NaN</code>.
</p><p>For arithmetic purposes, the <code>NaN</code> value is not a number in any radix. You can call the <code><a href="en/Core_JavaScript_1.5_Reference/Global_Functions/isNaN">isNaN</a></code> function to determine if the result of <code>parseInt</code> is <code>NaN</code>. If <code>NaN</code> is passed on to arithmetic operations, the operation results will also be <code>NaN</code>.
</p>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Using_parseInt"> Example: Using <code>parseInt</code> </h4>
<p>The following examples all return 15:
</p>
<ul><li><code>parseInt("F", 16);</code>
</li><li><code>parseInt("17", 8);</code>
</li><li><code>parseInt("15", 10);</code>
</li><li><code>parseInt(15.99, 10);</code>
</li><li><code>parseInt("FXX123", 16);</code>
</li><li><code>parseInt("1111", 2);</code>
</li><li><code>parseInt("15*3", 10);</code>
</li><li><code>parseInt("12", 13);</code>
</li></ul>
<p>The following examples all return <code>NaN</code>:
</p>
<ul><li><code>parseInt("Hello", 8); // Not a number at all </code> 
</li><li><code>parseInt("0x7", 10);  // Not in base 10 format </code> 
</li><li><code>parseInt("546", 2);  // Digits are not valid for binary representations</code>  
</li></ul>
<p>Even though the radix is specified differently, the following examples all return 17 because the input <code>string</code> begins with "<code>0x</code>".
</p>
<ul><li><code>parseInt("0x11", 16);</code>
</li><li><code>parseInt("0x11", 0);</code>
</li><li><code>parseInt("0x11");</code>
</li></ul>
<h3 name="See_also"> See also </h3>
<p><a href="en/Core_JavaScript_1.5_Reference/Global_Functions/isNaN">isNaN</a>,
<a href="en/Core_JavaScript_1.5_Reference/Global_Functions/parseFloat">parseFloat</a>,
<a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Object/valueOf">Object.valueOf</a>
</p>
Revert to this revision