| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/parseInt | [Back] [Original] |
Get to know MDN better
parseInt(string, radix) radix 2-36
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);
radix 2 36parseInt('123', 5) // '123' 5 38 => 1*5^2 + 2*5^1 + 3*5^0 = 38
parseInt NaN
NaN radix (radix 10 8 "07"16"0xff")
radix 10 9 16 A F
parseInt radix parseInt
e 6.02223 6.022e23 parseInt parseInt Math.floor()
parseInt + - ECMAScript 1
radix undefined0 JavaScript
string 0x 0X 0 X radix 16string "0"0radix 8 10 radix ECMAScript 5 10 () parseInt radixstring radix 10 ()parseInt NaN
NaN radix isNaN parseInt NaN NaN NaN
radix thatNumber.toString(radix)
parseInt15:
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); //
parseInt("546", 2); // 01
-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); // 4
parseInt(0.00000000000434, 10); // 4
224
parseInt("0e0", 16);
radix ECMAScript 3 ECMAScript 5 0 numeric stringradix
parseInt("0e0");
// 0
parseInt("08");
// 0, '8'
ECMAScript 5 parseInt 0 ECMAScript 5
radixparseInt radix 0 10 0x 0X 16
ECMAScript 3 ECMAScript 3
2013 radix
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
| ECMAScript 2027 LanguageSpecification # sec-parseint-string-radix |
| Web Proxy Viewer | New URL | Original Page |