如何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
parseInt("010"); // 8, implicit octal
parseInt("010", 10); // 10, decimal radix used
但是它可以用十六进制表示法处理数字,就像parseInt
:
Number("0xF"); // 15
parseInt("0xF"); //15
另外,用于执行数值类型转换的一种广泛使用的构造是一元+
运算符(p。72 ),它等效于将Number
构造函数用作函数:
+"2e1"; // 20
+"0xF"; // 15
+"010"; // 10
typeof parseInt("123") => number
typeof Number("123") => number
typeof new Number("123") => object (Number primitive wrapper object)
前两个会返回原始值而不是对象,因此会提高性能。
如果您正在寻找性能,那么按位右移可能会获得最佳效果"10">>0
。还要乘("10" * 1
)或不乘(~~"10"
)。所有这些都比Number
和快得多parseInt
。他们甚至具有“功能”,而不是数字参数返回0。这是性能测试。
我发现性能的两个链接在转换string
为的几种方法中进行了比较int
。
parseInt(str,10)
parseFloat(str)
str << 0
+str
str*1
str-0
Number(str)
http://phrogz.net/js/string_to_number.html
一个微小的差别是他们转换什么undefined
或者null
,
Number() Or Number(null) // returns 0
而
parseInt() Or parseInt(null) // returns NaN
概要:
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
我一直使用parseInt,但是要注意前导零,这将迫使它进入八进制模式。
parseInt()
->将数字解析为指定的redix。
Number()
->将指定的值转换为等效的数值或NaN(如果操作失败)。
因此,为了将一些非数字值转换为数字,我们应该始终使用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
parseInt()
在进行redix转换时,函数会遇到各种极端情况,因此我们应避免出于强制目的使用parseInt()函数。
现在,要检查天气提供的值是否为数字,我们应该使用本机isNaN()
函数
parseInt转换为整数,即去除小数。数字不会转换为整数。
除非需要十六进制或八进制,否则最好不要使用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
与其冒险冒险发现这个错误以及其他提到的陷阱,不如不使用parseInt,除非您需要解析除base 10以外的内容,否则Number,Math.round,Math.foor和.toFixed(0)都可以做同样的事情parseInt可以用于没有这些类型的错误。
如果您确实希望或需要将parseInt用于其他一些特性,请不要使用它将浮点数转换为int。
文章标签:javascript , performance
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!