在将字符串转换为数字时,parseInt()和Number()的行为如何不同?
typeof parseInt("123") => number
typeof Number("123") => number
typeof new Number("123") => object (Number primitive wrapper object)
前两个将提供更好的性能,因为它返回一个原语而不是一个对象。
嗯,它们在语义上是不同的,作为函数调用的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
如果你正在寻找性能,那么最好的结果可能是按位右移“10”>>0。也可以乘(“10”* 1)或not not(~~“10”)。它们都比Number和parseInt快得多。 他们甚至有“feature”为非数字参数返回0。 下面是性能测试。
我发现在几种将字符串转换为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
一个微小的区别是它们转换为undefined或null,
Number() Or Number(null) Or Number('') // returns 0
而
parseInt() Or parseInt(null) // returns NaN
parseInt() ->解析一个数字到指定的redix。
Number()->如果转换失败,则将指定值转换为其等效数值或NaN。
因此,要将一些非数字值转换为数字,我们应该始终使用number()函数。
eg.
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
parseInt()函数有各种各样的极端情况,因为它进行重定向转换,因此我们应该避免使用parseInt()函数进行强制转换。
现在,要检查所提供的值是否是Numeric,我们应该使用nativeisNaN()函数
简介:
方法用于():
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而使用Number和Math是一个好主意。除非你需要十六进制或八进制。两者都可以使用字符串。为什么要远离它?
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
与其冒着很难找到这个错误和人们提到的其他陷阱的风险,我只会避免parseInt,除非你需要解析除10进制以外的东西。数,数学。圆的,数学。和. tofixed(0)都可以做同样的事情,parseInt可以用来没有这些类型的错误。
如果你真的想要或需要使用parseInt来实现它的其他特性,千万不要用它来将浮点数转换为整数。
获得结果的另一种方法是使用~操作符
在大多数情况下
~~someThing === parseInt(something)
但是~~对于parseInt接受的带尾字符的字符串或带基数规范的字符串(例如十六进制)将返回0,并且当parseInt返回NaN时也将返回0。另一个区别是~~如果给定一个bigint,则返回一个你可以添加另一个bigint的bigint,而parseInt如果bigint很大,则返回一个普通的浮点数(是的,它给出的值与parseFloat完全相同)
但是在大多数情况下~~比parseInt快30%。当浮点数表示为字符串时,它只会慢10%。
因此,如果~~的限制范围更适合你的需要,那么就节省使用电脑的时间,减少打字的时间