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


当前回答

简介:

方法用于():

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

其他回答

一个微小的区别是它们转换为undefined或null,

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

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

我总是使用parseInt,但要注意前导零会迫使它进入八进制模式。

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

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

远离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来实现它的其他特性,千万不要用它来将浮点数转换为整数。

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()函数