javascript 如何将两个数字相加?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16292523/
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 add two numbers?
提问by Pramito Rahman
I wrote a JavaScript calculator... but suppose when I give my first number as 2
and the second number as 3
, the result is showing 23
, but I wanted to add the two numbers.
我写了一个 JavaScript 计算器……但是假设当我给我的第一个数字 as2
和第二个数字 as 时3
,结果显示为23
,但我想将这两个数字相加。
Can anyone please help me? It is also happening when I try to minus the two numbers. Why isn't this working?
谁能帮帮我吗?当我尝试减去这两个数字时,也会发生这种情况。为什么这不起作用?
var cal = prompt("Please enter what type of calculation you want to do\n
if you wanna add enter = 1\n
if you want to minus enter = 2\n
if you want to divide enter = 3\n
if you want to multiply enter = 4");
if (cal == 1) {
var a = prompt("Please enter your first number");
var b = prompt("please enter your second number");
alert("The result is , " + a+b);
}
if (cal == 2) {
var c = prompt("Please enter your first number");
var d = prompt("please enter your second number");
alert("the result is , " + c - d);
}
采纳答案by Satpal
Try this:
试试这个:
var cal = prompt("Please enter what type of calculation you want to do\n" +
"if you want to add enter = 1\n" +
"if you want to minus enter = 2\n" +
"if you want to divide enter = 3\n" +
"if you want to multiply enter = 4");
if (cal == 1) {
var a = prompt("Please enter your first number");
var b = prompt("please enter your second number");
alert("The result is , " + (Number(a) + Number(b)));
}
else if (cal == 2) {
var c = prompt("Please enter your first number");
var d = prompt("please enter your second number");
alert("the result is , " + (Number(c) - Number(d)));
}
回答by Danny Beckett
The +
sign is used to concatenate strings together, not to add them together mathematically.
该+
符号用于将字符串连接在一起,而不是在数学上将它们相加。
You need to wrap your variables in parseInt()
, e.g.
您需要将变量包装在parseInt()
,例如
alert("The result is , " + parseInt(a)+parseInt(b));
回答by zerkms
Convert a string to a number after you accept it from user:
从用户接受字符串后,将字符串转换为数字:
a = parseInt(a, 10);
回答by Michael Robinson
Prompts return strings, you need to parse them as integers (you could also do floats using parseFloat
)
提示返回字符串,您需要将它们解析为整数(您也可以使用浮点数parseFloat
)
alert("The result is , " + (parseInt(a) + parseInt(b)));
alert("The result is , " + (parseInt(a) + parseInt(b)));
回答by Vijendra Singh
The Prompt
method returns the entered value as a string.
该Prompt
方法将输入的值作为字符串返回。
So after Prompt
use parseInt()
, this function parses a string and returns an integer.
所以在Prompt
使用之后parseInt()
,这个函数解析一个字符串并返回一个整数。
回答by PleaseStand
The binary+
operatorhas two uses: addition and string concatenation. Although you want the former, the latter is happening because window.prompt()
returns a string.
该二进制+
运算符有两个用途:加法和字符串连接。虽然你想要前者,但后者正在发生,因为window.prompt()
返回一个字符串。
To avoid this, you should do one of the following (read the documentation so you can understand the differences):
为避免这种情况,您应该执行以下操作之一(阅读文档以了解差异):
- Parse the strings as integers using
parseInt(str, 10)
. - Parse the strings as floating-point numbers using
parseFloat(str)
. - Cast the strings to numbers using the unaryplus operator:
+str
. - Alternatively,
Number(str)
can be used.
- 使用 将字符串解析为整数
parseInt(str, 10)
。 - 使用 将字符串解析为浮点数
parseFloat(str)
。 - 使用一元加号运算符将字符串转换为数字:
+str
。 - 或者,
Number(str)
可以使用。
It is wise to check whether the numbers could be parsed (using isNaN(num)
, or possibly num === num
) before trying to perform calculations with them, so your script can show a helpful error message instead of just carrying NaN
through to its output.
在尝试使用它们执行计算之前检查数字是否可以解析(使用isNaN(num)
或可能num === num
)是明智的,这样您的脚本可以显示有用的错误消息,而不仅仅是执行NaN
到其输出。
回答by Saiyam
Use:
利用:
var add = parseFloat(a)+parseFloat(b);
回答by zeeshan
num1 = window.prompt("Please Enter Your Num1 :");
num2 = window.prompt("Please Enter Your Num2 :");
var sum = parseInt(num1)+ parseInt(num2);
document.writeln("<h1> The summ of two integers,"+num1+", and,"+num2+", is ,"+sum+",</h1>" )
I Hope this will help you.
我希望这能帮到您。
回答by zeeshan
You are adding to Strings; known as concatenation, you need to convert the String representations to numbersthen add them.
您正在添加字符串;称为串联,您需要将字符串表示形式转换为数字,然后将它们相加。
alert("The result is , " + (parseInt(a) + parseInt(b)));
回答by Tamil Selvan C
Use
利用
alert("The result is , " + (parseFloat(a)+parseFloat(b)));