任何数字,就是数字。字符串看起来像一个数字,它是数字。其他所有内容都将继续保留。
'a' => NaN
'1' => 1
1 => 1
'1a'字符串应该怎么做?与' 1'一个?换句话说,为什么最常用的方法不足以做到这一点(Number(x)和parseInt(x, 10))?
任何数字,就是数字。字符串看起来像一个数字,它是数字。其他所有内容都将继续保留。
'a' => NaN
'1' => 1
1 => 1
'1a'字符串应该怎么做?与' 1'一个?换句话说,为什么最常用的方法不足以做到这一点(Number(x)和parseInt(x, 10))?
Answers:
据我所知,有4种方法可以做到这一点。
Number(x);
parseInt(x, 10);
parseFloat(x);
+x;
通过我所做的快速测试,它实际上取决于浏览器。
http://jsperf.com/best-of-string-to-number-conversion/2
Implicit 在3个浏览器上标记为最快,但是它使代码难以阅读...因此,请选择任何您喜欢的方式!
1*用于日期到数字的转换,与+上面的类似。即1*new Date()不是+new Date()。可能更具可读性吗?
1*它是首选,因为它不容易出错。之前不需要的悬挂变量+1不是解析错误。这是类似于使用技巧if (MYCONSTANT == myvar)在C。
如果只想转换为整数,则另一种快速(简短)的方法不是按位方式转换(即使用两个波浪号字符):
例如
~~x;参考:http : //james.padolsey.com/cool-stuff/double-bitwise-not/
到目前为止,我所知道的将字符串转换为数字的5种常见方式都有其差异(存在更多按位运算符,但它们的结果与相同~~)。此JSFiddle显示了您在调试控制台中可以预期的不同结果:http : //jsfiddle.net/TrueBlueAussie/j7x0q0e3/22/
var values = ["123",
undefined,
"not a number",
"123.45",
"1234 error",
"2147483648",
"4999999999"
];
for (var i = 0; i < values.length; i++){
var x = values[i];
console.log(x);
console.log(" Number(x) = " + Number(x));
console.log(" parseInt(x, 10) = " + parseInt(x, 10));
console.log(" parseFloat(x) = " + parseFloat(x));
console.log(" +x = " + +x);
console.log(" ~~x = " + ~~x);
}
123
Number(x) = 123
parseInt(x, 10) = 123
parseFloat(x) = 123
+x = 123
~~x = 123
undefined
Number(x) = NaN
parseInt(x, 10) = NaN
parseFloat(x) = NaN
+x = NaN
~~x = 0
null
Number(x) = 0
parseInt(x, 10) = NaN
parseFloat(x) = NaN
+x = 0
~~x = 0
"not a number"
Number(x) = NaN
parseInt(x, 10) = NaN
parseFloat(x) = NaN
+x = NaN
~~x = 0
123.45
Number(x) = 123.45
parseInt(x, 10) = 123
parseFloat(x) = 123.45
+x = 123.45
~~x = 123
1234 error
Number(x) = NaN
parseInt(x, 10) = 1234
parseFloat(x) = 1234
+x = NaN
~~x = 0
2147483648
Number(x) = 2147483648
parseInt(x, 10) = 2147483648
parseFloat(x) = 2147483648
+x = 2147483648
~~x = -2147483648
4999999999
Number(x) = 4999999999
parseInt(x, 10) = 4999999999
parseFloat(x) = 4999999999
+x = 4999999999
~~x = 705032703
该~~x版本在“更多”的情况下会产生一个数字,而其他情况通常会导致undefined,但由于无效输入而导致失败(例如,0如果字符串在有效数字之后包含非数字字符,它将返回)。
请注意:,可能会发生整数溢出和/或位截断~~,但其他转换则不会。虽然输入这么大的值并不常见,但是您需要意识到这一点。示例已更新为包含更大的值。
一些Perf测试表明标准parseInt和parseFloat功能实际上是最快的选项,可能是浏览器对其进行了高度优化,但这取决于您的要求,因为所有选项都足够快:http : //jsperf.com/best-of-string-to -number-conversion / 37
这一切都取决于性能测试的配置方式,因为一些show parseInt / parseFloat要慢得多。
~~4294967296return 0。
这是简单的方法: var num = Number(str); 在此示例中,str是包含字符串的变量。您可以对其进行测试并查看其工作方式:Google chrome开发人员工具,然后转到控制台并粘贴以下代码。阅读评论以更好地了解转换是如何完成的。
// Here Im creating my variable as a string
var str = "258";
// here im printing the string variable: str
console.log ( str );
// here Im using typeof , this tells me that the variable str is the type: string
console.log ("The variable str is type: " + typeof str);
// here is where the conversion happens
// Number will take the string in the parentesis and transform it to a variable num as type: number
var num = Number(str);
console.log ("The variable num is type: " + typeof num);
这可能没有那么快,但是具有确保您的数字至少是某个值(例如0)或最多是某个值的附加好处:
Math.max(input, 0);
如果您需要确保最小值,通常会
var number = Number(input);
if (number < 0) number = 0;
Math.max(..., 0) 使您不必编写两个语句。
Math.abs(input)?它还将字符串转换为正数,并节省一些额外的字符。
input不能转换为数字,则将获得NaN
您可以尝试使用UnitOf,这是我们刚刚正式发布的测量和数据类型转换库!UnitOf超级快,体积小并且可以高效地转换任何数据类型,而不会抛出错误或null / undefined。转换不成功时,将返回您定义的默认值或UnitOf的默认值。
//One liner examples
UnitOf.DataType("12.5").toFloat(); //12.5 of type Float is returned. 0 would be returned if conversion failed.
UnitOf.DataType("Not A Num").toInt(10); //10 of type Int is returned as the conversion failed.
//Or as a variable
var unit = UnitOf.DataType("12.5");
unit.toInt(5); //12.5 of type Float is returned. 5 would be returned if the conversion failed.
unit.toFloat(8); // 12 of type Int is returned. 8 would be returned if the conversion failed.