javascript 在javascript中将字符串转换为数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11613705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert string to a number in javascript
提问by Om3ga
I want to parse a user input which contains longitude and latitude. What I want to do is to coerce a string to a number, preserving its sign and decimal places. But what I want to do is to display a message when user's input is invalid. Which one should I follow
我想解析包含经度和纬度的用户输入。我想要做的是将字符串强制为数字,保留其符号和小数位。但是我想要做的是在用户输入无效时显示一条消息。我应该关注哪一个
parseFloat(x)
second
第二
new Number(x)
third
第三
~~x
fourth
第四
+x
回答by Pointy
I'd use Number(x)
, if I had to choose between those two, because it won't allow trailing garbage. (Well, it "allows" it, but the result is a NaN
.)
Number(x)
如果必须在这两者之间进行选择,我会使用,因为它不允许尾随垃圾。(好吧,它“允许”它,但结果是NaN
.)
That is, Number("123.45balloon")
is NaN
, but parseFloat("123.45balloon")
is 123.45
(as a number).
也就是说,Number("123.45balloon")
是NaN
,但是parseFloat("123.45balloon")
是123.45
(作为一个数字)。
As Mr. Kling points out, which of those is "better" is up to you.
正如克林先生指出的那样,哪些“更好”取决于您。
edit— ah, you've added back +x
and ~~x
. As I wrote in a comment, +x
is equivalent to using the Number()
constructor, but I think it's a little risky because of the syntactic flexibility of the +
operator. That is, it'd be easy for a cut-and-paste to introduce an error. The ~~x
form is good if you know you want an integer (a 32-bit integer) anyway. For lat/long that's probably not what you want however.
编辑- 啊,你又加回来了+x
和~~x
。正如我在评论中写的,+x
相当于使用Number()
构造函数,但我认为由于+
操作符的语法灵活性,它有点冒险。也就是说,剪切和粘贴很容易引入错误。~~x
如果你知道你想要一个整数(一个 32 位整数),那么这个形式就很好。然而,对于经纬度,这可能不是您想要的。
回答by Ates Goral
The first one is better. It is explicit, and it is correct. You said you want to parse floating point numbers. ~~x
will give you an integer.
第一个更好。这是明确的,也是正确的。你说你想解析浮点数。~~x
会给你一个整数。
回答by Engineer
To test whether input
is number, use this:
要测试是否input
为数字,请使用以下命令:
function isNumeric(obj){
return !isNaN( parseFloat(obj) ) && isFinite( obj );
}
To cast String
to Number
, use +
,it's the fastest method:
要强制转换String
为Number
,使用+
,这是最快的方法:
the unary + operator also type-converts its operand to a number and because it does not do any additional mathematical operations it is the fastest method for type-converting a string into a number
一元 + 运算符还将其操作数类型转换为数字,并且由于它不执行任何额外的数学运算,因此它是将字符串类型转换为数字的最快方法
So overall, probably you need this:
所以总的来说,可能你需要这个:
if(!isNumeric(inputValue)){
alert('invalid number');
}else{
var num = +inputValue;
}
isNumeric
borrowed from jQuery
isNumeric
借来的 jQuery
回答by M. Ahmad Zafar
This is the code I would write to repeatedly take input until the correct one is obtained.
这是我将编写的代码以重复输入,直到获得正确的输入。
var d;
do {
d = prompt("Enter a number");
d = new Number(d);
} while( isNaN(d) );
document.write( d );
Note:new Number(d)
will always give NaN
if any character is non-numeric while parseFloat(d)
will ignore trailing invalid characters.
注意:如果任何字符是非数字,new Number(d)
将始终给出NaN
,而parseFloat(d)
将忽略尾随无效字符。