此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。
parseInt()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
parseInt() 函式能將輸入的字串轉成整數。
In this article
嘗試一下
console.log(parseInt("123"));// 123 (default base-10)console.log(parseInt("123", 10));// 123 (explicitly specify base-10)console.log(parseInt(" 123 "));// 123 (whitespace is ignored)console.log(parseInt("077"));// 77 (leading zeros are ignored)console.log(parseInt("1.9"));// 1 (decimal part is truncated)console.log(parseInt("ff", 16));// 255 (lower-case hexadecimal)console.log(parseInt("0xFF", 16));// 255 (upper-case hexadecimal with "0x" prefix)console.log(parseInt("xyz"));// NaN (input can't be converted to an integer)語法
parseInt(string, radix);參數
回傳值
藉由給定字串作轉換後的數字。若第一個字符無法轉換為數字,則回傳NaN。
說明
parseInt 函式會把第一個參數變成字串、解析它、再回傳整數或是NaN。如果不是NaN,回傳值會把第一個參數,參照指定的radix 後,以十進位表示。例如,radix 指定為 10 的話,它會以十進位為單位轉換、8 是八進位、16 是十六進位,依此類推。For radices above10, the letters of the alphabet indicate numerals greater than9. For example, for hexadecimal numbers (base 16),A throughF are used.
如果說parseInt 碰上了無法被 radix 指定的進位制所轉換的字元,它會忽略該字元、以及其後所有字元,並只回傳至該位置為止的解析數值結果。parseInt 將數字擷取、轉換成整數數值。 可以接受字串首尾出現空白。
Because some numbers include thee character in their string representation (e.g.6.022e23), usingparseInt to truncate numeric values will produce unexpected results when used on very large or very small numbers.parseInt should not be used as a substitute forMath.floor().
如果radix 是undefined 或 0(或留空)的話,JavaScript 會:
- 如果
string由 "0x" 或 "0X" 開始,radix 會變成代表十六進位的 16,並解析字串的餘數。 - 如果
string由 0 開始,則radix 會變成代表八進位的 8 或十進位的 10,但到底會變成 8 還是 10 則取決於各實做。ECMAScript 規定用代表十進位的 10,但也不是所有瀏覽器都支持。因此,使用parseInt時一定要指定 radix。 - 如果
string由其他字串開始,radix 就會是十進位的 10。
如果第一個字串無法被解析為任何數字,parseInt 會回傳NaN。
For arithmetic purposes, theNaN value is not a number in any radix. You can call theisNaN function to determine if the result ofparseInt isNaN. IfNaN is passed on to arithmetic operations, the operation results will also beNaN.
若想將數字轉成特定的進位制,可使用intValue.toString(radix)。
範例
>使用parseInt
以下的範例,回傳的值均為15:
parseInt(" 0xF", 16);parseInt(" F", 16);parseInt("17", 8);parseInt(021, 8);parseInt("015", 10); // parseInt(015, 10); will return 15parseInt(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); // 根本不是數字parseInt("546", 2); // 在二進位無效以下的範例,回傳的值均為-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);下例會回傳4:
parseInt(4.7, 10);parseInt(4.7 * 1e22, 10); // Very large number becomes 4parseInt(0.00000000000434, 10); // Very small number becomes 4下例會回傳224:
parseInt("0e0", 16);無 radix 情況下的八進制
雖說已在 ECMAScript 3 提議並於 ECMAScript 5 禁用,但部分 javascript 編譯器仍會在特殊情況下,將 str 視作八進位數字(當數字以0 開頭時)。以下為可能發生這種問題的情況:(永遠要宣告 radix 以避開這不可靠的行為)
parseInt("0e0"); // 0parseInt("08"); // 0, '8' is not an octal digit.ECMAScript 5 移除八進位轉譯(octal interpretation)
The ECMAScript 5 specification of the functionparseInt no longer allows implementations to treat Strings beginning with a0 character as octal values. ECMAScript 5 states:
TheparseInt 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 or0, it is assumed to be10 except when the number begins with the character pairs0x or0X, 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 expression 可以幫你:
filterInt = function (value) { if (/^(\-|\+)?([0-9]+|Infinity)$/.test(value)) return Number(value); return NaN;};console.log(filterInt("421")); // 421console.log(filterInt("-421")); // -421console.log(filterInt("+421")); // 421console.log(filterInt("Infinity")); // Infinityconsole.log(filterInt("421e+0")); // NaNconsole.log(filterInt("421hop")); // NaNconsole.log(filterInt("hop1.61803398875")); // NaNconsole.log(filterInt("1.61803398875")); // NaN規範
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-parseint-string-radix> |