在将字符串转换为数字时,parseInt()和Number()的行为如何不同?


当前回答

嗯,它们在语义上是不同的,作为函数调用的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

其他回答

简介:

方法用于():

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,但要注意前导零会迫使它进入八进制模式。

我发现在几种将字符串转换为int的方法中有两个性能比较链接。

    parseInt(str,10)    
    parseFloat(str)
    str << 0
    +str
    str*1 
    str-0
    Number(str)

http://jsben.ch/#/zGJHM

http://phrogz.net/js/string_to_number.html

parseInt转换为整数,也就是说,它去掉小数。数字不转换为整数。

typeof parseInt("123") => number
typeof Number("123") => number
typeof new Number("123") => object (Number primitive wrapper object)

前两个将提供更好的性能,因为它返回一个原语而不是一个对象。