parseInt and parseFloat Functions

  • Revision slug: JavaScript/Guide/Obsolete_Pages/Predefined_Functions/parseInt_and_parseFloat_Functions
  • Revision title: parseInt and parseFloat Functions
  • Revision id: 117987
  • Created:
  • Creator: user01
  • Is current revision? No
  • Comment Merged into "Functions"; 20 words added

Revision Content

{{ mergedInto("https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Functions#parseInt_and_parseFloat_Functions") }}

parseInt and parseFloat Functions

The two "parse" functions, parseInt and parseFloat, return a numeric value when given a string as an argument.

The syntax of parseFloat is

parseFloat(str)

where parseFloat parses its argument, the string str, and attempts to return a floating-point number. If it encounters a character other than a sign (+ or -), a numeral (0-9), a decimal point, or an exponent, then it returns the value up to that point and ignores that character and all succeeding characters. If the first character cannot be converted to a number, it returns "NaN" (not a number).

The syntax of parseInt is

parseInt(str [, radix])

parseInt parses its first argument, the string str, and attempts to return an integer of the specified radix (base), indicated by the second, optional argument, radix. For example, a radix of ten indicates to convert to a decimal number, eight octal, sixteen hexadecimal, and so on. For radixes above ten, the letters of the alphabet indicate numerals greater than nine. 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. If the first character cannot be converted to a number in the specified radix, it returns "NaN." The parseInt function truncates the string to integer values.

{{ PreviousNext("Core_JavaScript_1.5_Guide:Predefined_Functions:isNaN_Function", "Core_JavaScript_1.5_Guide:Predefined_Functions:Number_and_String_Functions") }}

{{ languages( { "zh-tw": "zh_tw/Core_JavaScript_1.5_教學/預先定義的函數/Number_和_String_函數", "es": "es/Gu\u00eda_JavaScript_1.5/Funciones_predefinidas/Funciones_parseInt_y_parseFloat", "fr": "fr/Guide_JavaScript_1.5/Fonctions_pr\u00e9d\u00e9finies/Les_fonctions_parseInt_et_parseFloat", "ja": "ja/Core_JavaScript_1.5_Guide/Predefined_Functions/parseInt_and_parseFloat_Functions", "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Funkcje_predefiniowane/Funkcje_parseInt_i_parseFloat" } ) }}

Revision Source

<p>{{ mergedInto("https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Functions#parseInt_and_parseFloat_Functions") }}</p>
<div class="onlyinclude">
<h4>parseInt and parseFloat Functions</h4>
<p>The two "parse" functions, <code>parseInt</code> and <code>parseFloat</code>, return a numeric value when given a string as an argument.</p>
<p>The syntax of <code>parseFloat</code> is</p>
<pre>parseFloat(str)
</pre>
<p>where <code>parseFloat</code> parses its argument, the string <code>str</code>, and attempts to return a floating-point number. If it encounters a character other than a sign (+ or -), a numeral (0-9), a decimal point, or an exponent, then it returns the value up to that point and ignores that character and all succeeding characters. If the first character cannot be converted to a number, it returns "NaN" (not a number).</p>
<p>The syntax of <code>parseInt</code> is</p>
<pre>parseInt(str [, radix])
</pre>
<p><code>parseInt</code> parses its first argument, the string <code>str</code>, and attempts to return an integer of the specified <code>radix</code> (base), indicated by the second, optional argument, <code>radix</code>. For example, a radix of ten indicates to convert to a decimal number, eight octal, sixteen hexadecimal, and so on. For radixes above ten, the letters of the alphabet indicate numerals greater than nine. For example, for hexadecimal numbers (base 16), A through F are used.</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. If the first character cannot be converted to a number in the specified radix, it returns "NaN." The <code>parseInt</code> function truncates the string to integer values.</div>
<p>{{ PreviousNext("Core_JavaScript_1.5_Guide:Predefined_Functions:isNaN_Function", "Core_JavaScript_1.5_Guide:Predefined_Functions:Number_and_String_Functions") }}</p>
<p>{{ languages( { "zh-tw": "zh_tw/Core_JavaScript_1.5_教學/預先定義的函數/Number_和_String_函數", "es": "es/Gu\u00eda_JavaScript_1.5/Funciones_predefinidas/Funciones_parseInt_y_parseFloat", "fr": "fr/Guide_JavaScript_1.5/Fonctions_pr\u00e9d\u00e9finies/Les_fonctions_parseInt_et_parseFloat", "ja": "ja/Core_JavaScript_1.5_Guide/Predefined_Functions/parseInt_and_parseFloat_Functions", "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Funkcje_predefiniowane/Funkcje_parseInt_i_parseFloat" } ) }}</p>
Revert to this revision