Javascript 如何强制 JS 做数学运算而不是将两个字符串放在一起
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4841373/
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
How to force JS to do math instead of putting two strings together
提问by Sean
I need javascript to add 5 to an integer variable, but instead it treats the variable as a string, so it write out the variable, then add 5 onto the end of the "string". How can I force it to do math instead?
我需要 javascript 将 5 添加到一个整数变量中,但它将变量视为字符串,因此它写出变量,然后将 5 添加到“字符串”的末尾。我怎么能强迫它做数学呢?
var dots = document.getElementById("txt").value; // 5
function increase(){
dots = dots + 5;
}
Output: 55
输出: 55
How can I force it to output 10
?
我怎样才能强制它输出10
?
回答by Alex
You have the line
你有这条线
dots = document.getElementById("txt").value;
in your file, this will set dots to be a string because the contents of txt is not restricted to a number.
在您的文件中,这会将点设置为字符串,因为 txt 的内容不限于数字。
to convert it to an int change the line to:
要将其转换为 int 将行更改为:
dots = parseInt(document.getElementById("txt").value, 10);
Note: The 10
here specifies decimal (base-10). Without this some browsers may not interpret the string correctly. See MDN: parseInt
.
注意:10
此处指定十进制(基数为 10)。如果没有这个,一些浏览器可能无法正确解释字符串。见MDN: parseInt
。
回答by mier
the simplest:
最简单的:
dots = dots*1+5;
the dots will be converted to number.
点将被转换为数字。
回答by Nicolas
DON'T FORGET - Use parseFloat();
if your dealing with decimals.
不要忘记 -parseFloat();
如果您处理小数,请使用。
回答by Christopher Strolia-Davis
I'm adding this answer because I don't see it here.
我添加这个答案是因为我在这里没有看到它。
One way is to put a '+' character in front of the value
一种方法是在值前面放一个“+”字符
example:
例子:
var x = +'11.5' + +'3.5'
x === 15
x === 15
I have found this to be the simplest way
我发现这是最简单的方法
In this case, the line:
在这种情况下,该行:
dots = document.getElementById("txt").value;
could be changed to
可以改为
dots = +(document.getElementById("txt").value);
to force it to a number
将其强制为一个数字
NOTE:
笔记:
+'' === 0
+[] === 0
+[5] === 5
+['5'] === 5
回答by Alan L.
parseInt()
should do the trick
parseInt()
应该做的伎俩
var number = "25";
var sum = parseInt(number, 10) + 10;
var pin = number + 10;
Gives you
给你
sum == 35
pin == "2510"
http://www.w3schools.com/jsref/jsref_parseint.asp
http://www.w3schools.com/jsref/jsref_parseint.asp
Note: The 10
in parseInt(number, 10)
specifies decimal (base-10). Without this some browsers may not interpret the string correctly. See MDN: parseInt
.
注意:10
inparseInt(number, 10)
指定十进制(基数为 10)。如果没有这个,一些浏览器可能无法正确解释字符串。见MDN: parseInt
。
回答by Holger.Buick
This also works for you:
这也适用于您:
dots -= -5;
回答by Nelson Cajetin Diego Alfonso
its really simple just
它真的很简单
var total = (1 * yourFirstVariablehere) + (1 * yourSecondVariablehere)
this forces javascript to multiply because there is no confusion for * sign in javascript.
这会强制 javascript 成倍增加,因为 * 登录 javascript 没有混淆。
回答by Hassan Ali Salem
You can add + behind the variable and it will force it to be an integer
您可以在变量后面添加 + ,它会强制它是一个整数
var dots = 5
function increase(){
dots = +dots + 5;
}
回答by Jay Reeve
After trying most of the answers here without success for my particular case, I came up with this:
在针对我的特定案例尝试了大部分答案但没有成功之后,我想出了这个:
dots = -(-dots - 5);
The + signs are what confuse js, and this eliminates them entirely. Simple to implement, if potentially confusing to understand.
+ 符号使 js 感到困惑,这完全消除了它们。实施起来很简单,但可能会让人难以理解。
回答by vapcguy
UPDATED since this was last downvoted....
自上次被否决以来已更新....
I only saw the portion
我只看到了部分
var dots = 5
function increase(){
dots = dots+5;
}
before, but it was later shown to me that the txt
box feeds the variable dots
. Because of this, you will need to be sure to "cleanse" the input, to be sure it only has integers, and not malicious code.
之前,但后来向我展示了该txt
框提供了变量dots
. 因此,您需要确保“清理”输入,以确保它只有整数,而不是恶意代码。
One easy way to do this is to parse the textbox with an onkeyup()
event to ensure it has numeric characters:
一种简单的方法是使用onkeyup()
事件解析文本框以确保它具有数字字符:
<input size="40" id="txt" value="Write a character here!" onkeyup="GetChar (event);"/>
where the event would give an error and clear the last character if the value is not a number:
如果值不是数字,事件将给出错误并清除最后一个字符:
<script type="text/javascript">
function GetChar (event){
var keyCode = ('which' in event) ? event.which : event.keyCode;
var yourChar = String.fromCharCode();
if (yourChar != "0" &&
yourChar != "1" &&
yourChar != "2" &&
yourChar != "3" &&
yourChar != "4" &&
yourChar != "5" &&
yourChar != "6" &&
yourChar != "7" &&
yourChar != "8" &&
yourChar != "9")
{
alert ('The character was not a number');
var source = event.target || event.srcElement;
source.value = source.value.substring(0,source.value-2);
}
}
</script>
Obviously you could do that with regex, too, but I took the lazy way out.
显然你也可以用正则表达式来做到这一点,但我采取了懒惰的方式。
Since then you would know that only numbers could be in the box, you should be able to just use eval()
:
从那时起,您就会知道框中只能包含数字,您应该可以使用eval()
:
dots = eval(dots) + 5;