parseInt()和Number()有什么区别?

时间:2010-11-03 18:48:17

标签: javascript performance

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

11 个答案:

答案 0 :(得分:400)

嗯,他们语义不同Number constructor called as a function执行类型转换parseInt执行解析,例如:

// parsing:
parseInt("20px");       // 20
parseInt("10100", 2);   // 20
parseInt("2e1");        // 2

// type conversion
Number("20px");       // NaN
Number("2e1");        // 20, exponential notation

请记住,如果parseInt检测到字符串上的前导零,它将解析八进制数中的数字,这在标准的新版本ECMAScript 5上已经更改,但这需要很长时间进入浏览器实现的时间(与ECMAScript 3不兼容),parseInt也将忽略与当前使用的数字的任何数字不对应的尾随字符。

Number构造函数未检测到octals:

Number("010");         // 10
parseInt("010");       // 8, implicit octal
parseInt("010", 10);   // 10, decimal radix used

但它可以用十六进制表示法处理数字,就像parseInt

一样
Number("0xF");   // 15
parseInt("0xF"); //15

此外,一个广泛使用的构造来执行数值类型转换,是Unary + Operator (p. 72),它相当于使用Number构造函数作为函数:

+"2e1";   // 20
+"0xF";   // 15
+"010";   // 10

答案 1 :(得分:20)

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

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

答案 2 :(得分:15)

如果您正在寻找性能,那么可能会获得最佳结果,您将获得按位右移"10">>0。还要乘以("10" * 1)或不乘(~~"10")。所有这些都比NumberparseInt快得多。 他们甚至有“功能”返回0表示没有数字参数。 以下是Performance tests

答案 3 :(得分:8)

我找到了两种将string转换为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

答案 4 :(得分:5)

我总是使用parseInt,但要注意会导致它进入 octal 模式的前导零。

答案 5 :(得分:5)

摘要:

parseInt()

  • 将字符串作为第一个参数,将基数(整数作为数字系统的基数,例如十进制10或二进制2)作为第二个参数
  • 如果第一个字符不能转换为数字NaN,则该函数返回一个整数。
  • 如果parseInt()函数遇到一个非数值,则它将截断其余的输入字符串,仅解析该部分直到该非数值。
  • 如果基数为undefined或0,则JS将假定以下内容:
    • 如果输入字符串以“ 0x”或“ 0X”开头,则基数为16(十六进制),则字符串的其余部分将解析为数字。
    • 如果输入值以0开头,则基数可以是8(八进制)或10(十进制)。选择哪个基数取决于JS引擎的实现。 ES5指定随后应使用10。但是,并非所有浏览器都支持此功能,因此因此,如果您的数字可以以0开头,则始终指定基数。
    • 如果输入值以任何数字开头,则基数将为10

Number()

  • 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

答案 6 :(得分:4)

一个小差异是他们转换为undefinednull

Number() Or Number(null) // returns 0

parseInt() Or parseInt(null) // returns NaN

答案 7 :(得分:1)

parseInt转换为整数,即删除小数。数字不会转换为整数。

答案 8 :(得分:1)

BatchJobs - &gt;将数字解析为指定的redix。 parseInt() - &gt;如果指定的值无法执行,则将指定的值转换为数字等效值或NaN。

因此,为了将一些非数字值转换为数字,我们应该始终使用Number()函数。

例如

Number()

Number("")//0 parseInt("")//NaN Number("123")//123 parseInt("123")//123 Number("123ac") //NaN,as it is a non numeric string parsInt("123ac") //123,it parse decimal number outof string Number(true)//1 parseInt(true) //NaN 函数存在各种角点,因为它进行了redix转换,因此我们应该避免使用parseInt()函数来进行coersion目的。 现在,要检查天气,提供的值是否为数字,我们应该使用原生parseInt()函数

答案 9 :(得分:0)

除非您需要十六进制或八进制,否则最好不要使用parseInt并使用Number和Math.round。两者都可以使用字符串。为什么要远离它?

parseInt(0.001, 10)
0

parseInt(-0.0000000001, 10)
-1

parseInt(0.0000000001, 10)
1

parseInt(4000000000000000000000, 10)
4

它完全可以屠杀数量庞大或数量很少的屠夫。奇怪的是,如果这些输入是字符串,它可以正常工作。

parseInt("-0.0000000001", 10)
0

parseInt("0.0000000001", 10)
0

parseInt("4000000000000000000000", 10)
4e+21

除非您需要解析除base 10之外的其他内容,否则我将避免使用parseInt,而不是冒险使用它以及其他提到的陷阱,而无需冒险寻找错误。Number,Math.round,Math.foor和.toFixed(0 )都可以做parseInt可以使用的相同功能,而不会遇到此类错误。

如果您确实希望或需要将parseInt用于其他一些特性,请不要使用它将浮点数转换为int。

答案 10 :(得分:0)

由于没有提及,将NumberparseInt与数字分隔符一起使用时,它们的行为也有所不同:

const num1 = 5_0; // 50
const num2 = Number(5_0); // 50
const num3 = Number("5_0"); // NaN
const num4 = parseInt(5_0); // 50
const num5 = parseInt("5_0"); // 5