Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

parseInt()

此文件需要編輯審查。看看您能幫什麼忙。

翻譯不完整。請協助 翻譯此英文文件

parseInt() function 能將輸入的字串轉成整數。

語法

parseInt(str, radix);

參數

參數 可選 默認值 類型 說明
str     String 待轉成數字的字串。若參數類型不是字串的話,會先將其轉成字串。
相當於先執行ToString再執行parseInt
radix 10 Number(正整數) 參數 str 為多少進位的數字。
默認視為 10 進位。
可接受的參數範圍為 2 至 36。

回傳值

轉換後的數字,為 Number 格式的正整數。

若無法轉換,則會回傳 NaN

說明

parseInt 函式會把第一個參數變成字串、解析它、再回傳整數或是 NaN。如果不是 NaN,回傳值會把第一個參數,參照指定的 radix 後,以十進位表示。例如,radix 指定為 10 的話,它會以十進位為單位轉換、8 是八進位、16 是十六進位,依此類推。For radices 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.

如果radixundefined 或 0(或沒寫), JavaScript 會假定:

  • If the input string begins with "0x" or "0X", radix is 16 (hexadecimal) and the remainder of the string is parsed.
  • If the input string begins with "0", radix is eight (octal) or 10 (decimal).  Exactly which radix is chosen is implementation-dependent.  ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet.  For this reason always specify a radix when using parseInt.
  • 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.

若想將數字轉成特定的進位制,可使用 intValue.toString(radix)

範例

Using parseInt

以下的範例,回傳的值均為 15:

parseInt(" 0xF", 16);
parseInt(" F", 16);
parseInt("17", 8);
parseInt(021, 8);
parseInt("015", 10);
parseInt(15.99, 10);
parseInt("15,123", 10);
parseInt("FXX123", 16);
parseInt("1111", 2);
parseInt("15*3", 10);
parseInt("15e2", 10);
parseInt("15px", 10);
parseInt("12", 13);

以下均回傳 NaN:

parseInt("Hello", 8); // Not a number at all
parseInt("546", 2);   // Digits are not valid for binary representations

以下的範例,回傳的值均為 -15:

parseInt("-F", 16);
parseInt("-0F", 16);
parseInt("-0XF", 16);
parseInt(-15.1, 10)
parseInt(" -17", 8);
parseInt(" -15", 10);
parseInt("-1111", 2);
parseInt("-15e1", 10);
parseInt("-12", 13);

下例會回傳 224:

parseInt("0e0", 16);

無 radix 情況下的八進制

雖說已在 ECMAScript 3 提議並於 ECMAScript 5 禁用,但部分 javascript 編譯器仍會在特殊情況下,將 str 視作八進位數字(當數字以  0 開頭時)。下為可能發生這種問題的情況:(  Always specify a radix to avoid this unreliable behavior.)

parseInt("0e0"); // 0
parseInt("08"); // 0, '8' is not an octal digit.

ECMAScript 5 移除八進位轉譯(octal interpretation)

The ECMAScript 5 specification of the function parseInt no longer allows implementations to treat Strings beginning with a 0 character as octal values. ECMAScript 5 states:

The parseInt function produces an integer value dictated by interpretation of the contents of the string argument according to the specified radix. Leading white space in string is ignored. If radix is undefined or 0, it is assumed to be 10 except when the number begins with the character pairs 0x or 0X, in which case a radix of 16 is assumed.

This differs from ECMAScript 3, which discouraged but allowed octal interpretation.

Many implementations have not adopted this behavior as of 2013, and because older browsers must be supported, always specify a radix.

嚴謹的解析 function

有的時候,使用更嚴謹的 code 能夠更精確地轉換整數值。 Regular expressions 可以幫你:

filterInt = function (value) {
  if(/^(\-|\+)?([0-9]+|Infinity)$/.test(value))
    return Number(value);
  return NaN;
}

console.log(filterInt('421'));               // 421
console.log(filterInt('-421'));              // -421
console.log(filterInt('+421'));              // 421
console.log(filterInt('Infinity'));          // Infinity
console.log(filterInt('421e+0'));            // NaN
console.log(filterInt('421hop'));            // NaN
console.log(filterInt('hop1.61803398875'));  // NaN
console.log(filterInt('1.61803398875'));     // NaN

定義

Specification Status Comment
ECMAScript 1st Edition (ECMA-262) Standard 初始定義
ECMAScript 5.1 (ECMA-262)
The definition of 'parseInt' in that specification.
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'parseInt' in that specification.
Standard  
ECMAScript 2017 Draft (ECMA-262)
The definition of 'parseInt' in that specification.
Draft  

瀏覽器支援度

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) ? (Yes)
Parses leading-zero strings as decimal, not octal (Yes) 21 (Yes) (in standards mode) ? (Yes)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)
Parses leading-zero strings as decimal, not octal ? ? 21 ? ? ?

延伸閱讀

文件標籤與貢獻者

 此頁面的貢獻者: Shiyou, iigmir
 最近更新: Shiyou,