How to add two strings as if they were numbers?

2018-12-31 03:36发布

I have two strings which contain only numbers:

var num1 = '20',
    num2 = '30.5';

I would have expected that I could add them together, but they are being concatenated instead:

num1 + num2; // = '2030.5'

How can I force these strings to be treated as numbers?

标签: javascript
18条回答
冷夜・残月
2楼-- · 2018-12-31 04:10

Here, you have two options to do this :-

1.You can use the unary plus to convert string number into integer.

2.You can also achieve this via parsing the number into corresponding type. i.e parseInt(), parseFloat() etc

.

Now I am going to show you here with the help of examples(Find the sum of two numbers).

Using unary plus operator

<!DOCTYPE html>
<html>
<body>

<H1>Program for sum of two numbers.</H1>
<p id="myId"></p>
<script>
var x = prompt("Please enter the first number.");//prompt will always return string value
var y = prompt("Please enter the second nubmer.");
var z = +x + +y;    
document.getElementById("myId").innerHTML ="Sum of "+x+" and "+y+" is "+z;
</script>
</body>
</html>

Using parsing approach-

<!DOCTYPE html>
<html>
<body>

<H1>Program for sum of two numbers.</H1>
<p id="myId"></p>
<script>
var x = prompt("Please enter the first number.");
var y = prompt("Please enter the second number.");   
var z = parseInt(x) + parseInt(y);
document.getElementById("myId").innerHTML ="Sum of "+x+" and "+y+" is "+z;
</script>
</body>
</html>
查看更多
柔情千种
3楼-- · 2018-12-31 04:12

I've always just subtracted zero.

num1-0 + num2-0;

Granted that the unary operator method is one less character, but not everyone knows what a unary operator is or how to google to find out when they don't know what it's called.

查看更多
初与友歌
4楼-- · 2018-12-31 04:14

try

var x = parseFloat(num1) + parseFloat(num2) ;

or, depending on your needs:

var x = parseInt(num1) + parseInt(num2) ;

http://www.javascripter.net/faq/convert2.htm

You might want to pick up the book Javascript: The Good Parts, by Douglas Crockford. Javascript has a rather sizeable colleciton of gotchas! This book goes a long way towards clarifying them. See also

and Mr. Crockford's excellent essay, Javascript: The World's Most Misunderstood Programming Language.

查看更多
步步皆殇っ
5楼-- · 2018-12-31 04:15

I would recommend to use the unary plus operator, to force an eventual string to be treated as number, inside parenthesis to make the code more readable like the following:

(+varname)

So, in your case it's:

var num1 = '20',
    num2 = '30.5';

var sum = (+num1) + (+num2);

// Just to test it
console.log( sum ); // 50.5
查看更多
荒废的爱情
6楼-- · 2018-12-31 04:16

convert the strings to floats with parseFloat(string) or to integers with parseInt(string)

查看更多
ら面具成の殇う
7楼-- · 2018-12-31 04:16

If you want to perform operation with numbers as strings (as in the case where numbers are bigger than 64bits can hold) you can use the big-integer library.

const bigInt = require('big-integer')
bigInt("999").add("1").toString() // output: "1000"
查看更多
登录 后发表回答