任何数字都是数字。看起来像数字的字符串也是数字。其他所有的东西都是NaN(非数字)。
'a' => NaN
'1' => 1
1 => 1
任何数字都是数字。看起来像数字的字符串也是数字。其他所有的东西都是NaN(非数字)。
'a' => NaN
'1' => 1
1 => 1
据我所知,有4种方法可以做到这一点。
Number(x);
parseInt(x, 10);
parseFloat(x);
+x;
通过我所做的这个快速测试,实际上取决于浏览器。
Implicit
在三种浏览器中标记为最快,但会使代码难以阅读...因此选择你喜欢的方式吧!
1*
进行日期到数字的转换,这与上面的+
类似,例如使用1*new Date()
而不是+new Date()
。可能这样更易读? - Matthew Wilcoxson1*
更好,因为它更少出错。在+1
之前出现的不需要的悬挂变量并不是解析错误。这类似于在C
中使用if (MYCONSTANT == myvar)
的技巧。 - TomasNumber()
和 ~~
,结果 Number()
赢了,尽管有时它们的表现几乎相同。 - Andrew如果您只想转换为整数,另一种快速(且简短)的方法是使用双重位非(即使用两个波浪线字符):
例如:
~~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
。
请注意:使用~~
可能会导致整数溢出和/或位截断,但其他转换不会。虽然输入如此大的值是不寻常的,但您需要意识到这一点。示例已更新以包括更大的值。
一些性能测试表明,标准的parseInt
和parseFloat
函数实际上是最快的选项,可能被浏览器高度优化,但这取决于您的要求,因为所有选项都已足够快:http://jsperf.com/best-of-string-to-number-conversion/37
所有这些都取决于性能测试的配置,因为有些测试显示parseInt/parseFloat要慢得多。
~~4294967296
将返回0
。 - Joseph Goh使用+
运算符在字符串前加上前缀。
console.log(+'a') // NaN
console.log(+'1') // 1
console.log(+1) // 1
将字符串转换为整数的快速方法是使用按位或,如下所示:
x | 0
虽然这取决于实现方式,但从理论上讲,它应该相对快速(至少与+x
一样快),因为它首先会将x
转换为数字,然后执行非常高效的或运算。
以下是简单的方法: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);
num * 1
非常简单清晰,适用于整数和浮点数...这可能不是特别快的方法,但它具有确保数字至少达到某个值(例如0)或最多达到某个值的附加好处:
Math.max(input, 0);
var number = Number(input);
if (number < 0) number = 0;
Math.max(..., 0)
可以让你只用一句话实现原本需要两句话才能完成的操作。
Math.abs(input)
呢?它还可以将字符串转换为正数,并节省一些额外的字符。 - Aaron Gillionlet str = "43.2"
Number(str)
=> 43.2
2. parseInt(str)
=> 43
3. parseFloat(str)
=> 43.2
4. +str
=> 43
5. str * 1
=> 43.2
6. Math.floor(str)
=> 43
7. ~~str
=> 43//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.
最快的方法是使用-0:
const num = "12.34" - 0;
'1a'
字符串应该怎么处理?' 1'
呢?换句话说,为什么通常使用的方法(Number(x)
和parseInt(x, 10)
)对您来说并不足够? - raina77ow