在将字符串转换为数字时,parseInt()和Number()的行为如何不同?
当前回答
获得结果的另一种方法是使用~操作符
在大多数情况下
~~someThing === parseInt(something)
但是~~对于parseInt接受的带尾字符的字符串或带基数规范的字符串(例如十六进制)将返回0,并且当parseInt返回NaN时也将返回0。另一个区别是~~如果给定一个bigint,则返回一个你可以添加另一个bigint的bigint,而parseInt如果bigint很大,则返回一个普通的浮点数(是的,它给出的值与parseFloat完全相同)
但是在大多数情况下~~比parseInt快30%。当浮点数表示为字符串时,它只会慢10%。
因此,如果~~的限制范围更适合你的需要,那么就节省使用电脑的时间,减少打字的时间
其他回答
简介:
方法用于():
Takes a string as a first argument, the radix (An integer which is the base of a numeral system e.g. decimal 10 or binary 2) as a second argument The function returns a integer number, if the first character cannot be converted to a number NaN will be returned. If the parseInt() function encounters a non numerical value, it will cut off the rest of input string and only parse the part until the non numerical value. If the radix is undefined or 0, JS will assume the following: If the input string begins with "0x" or "0X", the radix is 16 (hexadecimal), the remainder of the string is parsed into a number. If the input value begins with a 0 the radix can be either 8 (octal) or 10 (decimal). Which radix is chosen is depending on JS engine implementation. ES5 specifies that 10 should be used then. However, this is not supported by all browsers, therefore always specify radix if your numbers can begin with a 0. If the input value begins with any number, the radix will be 10
数量():
Number()构造函数可以将任何参数输入转换为数字。如果Number()构造函数不能将输入转换为数字,则返回NaN。 Number()构造函数也可以处理十六进制数,它们必须以0x开头。
例子:
console.log(parseInt('0xF', 16)); // 15 // z is no number, it will only evaluate 0xF, therefore 15 is logged console.log(parseInt('0xFz123', 16)); // because the radix is 10, A is considered a letter not a number (like in Hexadecimal) // Therefore, A will be cut off the string and 10 is logged console.log(parseInt('10A', 10)); // 10 // first character isnot a number, therefore parseInt will return NaN console.log(parseInt('a1213', 10)); console.log('\n'); // start with 0X, therefore Number will interpret it as a hexadecimal value console.log(Number('0x11')); // Cannot be converted to a number, NaN will be returned, notice that // the number constructor will not cut off a non number part like parseInt does console.log(Number('123A')); // scientific notation is allowed console.log(Number('152e-1')); // 15.21
我总是使用parseInt,但要注意前导零会迫使它进入八进制模式。
typeof parseInt("123") => number
typeof Number("123") => number
typeof new Number("123") => object (Number primitive wrapper object)
前两个将提供更好的性能,因为它返回一个原语而不是一个对象。
一个微小的区别是它们转换为undefined或null,
Number() Or Number(null) Or Number('') // returns 0
而
parseInt() Or parseInt(null) // returns NaN
嗯,它们在语义上是不同的,作为函数调用的Number构造函数执行类型转换,而parseInt执行解析,例如:
// parsing:
parseInt("20px"); // 20
parseInt("10100", 2); // 20
parseInt("2e1"); // 2
// type conversion
Number("20px"); // NaN
Number("2e1"); // 20, exponential notation
此外,parseInt将忽略与当前使用的基数中任何数字不对应的尾随字符。
Number构造函数不检测隐式八进制,但可以检测显式八进制符号:
Number("010"); // 10
Number("0o10") // 8, explicit octal
parseInt("010"); // 8, implicit octal
parseInt("010", 10); // 10, decimal radix used
它可以处理十六进制的数字,就像parseInt一样:
Number("0xF"); // 15
parseInt("0xF"); //15
此外,用于执行Numeric类型转换的一个广泛使用的构造是Unary + Operator(第72页),它相当于将Number构造函数用作函数:
+"2e1"; // 20
+"0xF"; // 15
+"010"; // 10