Baseline Widely available
parseInt()
å½å¼è½å°è¼¸å
¥çåä¸²è½ææ´æ¸ã
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)
èªæ³ 忏
string
å¾
è½ææ¸åçå串ãè¥ string
忏é¡å䏿¯å串çè©±ï¼æå
å°å
¶è½æå串ï¼ç¸ç¶æ¼å
å·è¡ ToString
åå·è¡ parseInt
ï¼ç©ºç½å¼æè¢«å¿½ç¥ã
radix
å¾ 2 å° 36ï¼è½ä»£è¡¨è©²é²ä½ç³»çµ±çæ¸åãä¾å¦èªªæå® 10
å°±çæ¼æå®åé²ä½ãä¸å®è¦å®ç¾©éå忏以é¿å
ä»äººçå°æãä¹å¥½é ä¼°å½å¼çè¡çºãå¦ææ²ææå® radix ç話ï¼çµ¦åºççµæææç
§å¯¦åä¸åèç°ï¼è«æ³¨æï¼é常é è¨å¼ä¸æ¯ 10 é²ä½ã
èç±çµ¦å®å串ä½è½æå¾çæ¸åãè¥ç¬¬ä¸ååç¬¦ç¡æ³è½æçºæ¸åï¼ååå³ 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.
å¦æèªª parseInt
碰ä¸äºç¡æ³è¢« radix æå®çé²ä½å¶æè½æçåå
ï¼å®æå¿½ç¥è©²åå
ã以åå
¶å¾ææåå
ï¼ä¸¦åªåå³è³è©²ä½ç½®çºæ¢çè§£ææ¸å¼çµæãparseInt
å°æ¸åæ·åãè½æææ´æ¸æ¸å¼ã å¯ä»¥æ¥åå串é¦å°¾åºç¾ç©ºç½ã
Because some numbers include the e
character in their string representation (e.g. 6.022e23
), using parseInt
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 for Math.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, 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)
ã
parseInt
以ä¸çç¯ä¾ï¼åå³çå¼åçº 15
ï¼
parseInt(" 0xF", 16);
parseInt(" F", 16);
parseInt("17", 8);
parseInt(021, 8);
parseInt("015", 10); // parseInt(015, 10); will return 15
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); // æ ¹æ¬ä¸æ¯æ¸å
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 4
parseInt(0.00000000000434, 10); // Very small number becomes 4
ä¸ä¾æåå³ 224
:
éèªªå·²å¨ ECMAScript 3 æè°ä¸¦æ¼ ECMAScript 5 ç¦ç¨ï¼ä½é¨å javascript ç·¨è¯å¨ä»æå¨ç¹æ®æ
æ³ä¸ï¼å° str è¦ä½å
«é²ä½æ¸åï¼ç¶æ¸å以 0
éé æï¼ã以ä¸çºå¯è½ç¼çé種åé¡çæ
æ³ï¼ï¼æ°¸é è¦å®£å radix 以é¿ééä¸å¯é çè¡çºï¼
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 expression å¯ä»¥å¹«ä½ ï¼
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
è¦ç¯ ç覽å¨ç¸å®¹æ§ åè¦
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4