How to Add Two Strings as If They Were Numbers

How to add two strings as if they were numbers?

I would use the unary plus operator to convert them to numbers first.

+num1 + +num2;

How to get the sum of two strings of numbers - C++

To add large integers using string, you can do something like this.

string doSum(string a, string b)
{
if(a.size() < b.size())
swap(a, b);

int j = a.size()-1;
for(int i=b.size()-1; i>=0; i--, j--)
a[j]+=(b[i]-'0');

for(int i=a.size()-1; i>0; i--)
{
if(a[i] > '9')
{
int d = a[i]-'0';
a[i-1] = ((a[i-1]-'0') + d/10) + '0';
a[i] = (d%10)+'0';
}
}
if(a[0] > '9')
{
string k;
k+=a[0];
a[0] = ((a[0]-'0')%10)+'0';
k[0] = ((k[0]-'0')/10)+'0';
a = k+a;
}
return a;
}

int main()
{
string result = doSum("1234567890", "123789456123");
cout << result << "\n";
}

Output

125024024013

Reference: See the complete code at Ideone.com

How to add two strings that are numbers?

As people allready answered this maybe another solution
So you don't get errors

    private static int AddTwoStrings(string one, string two) 
{
int iOne = 0;
int iTwo = 0;
Int32.TryParse(one, out iOne);
Int32.TryParse(two, out iTwo);
return iOne + iTwo;
}

Or if you want a string result.

private static String AddTwoStrings(string one, string two) 
{
int iOne = 0;
int iTwo = 0;
Int32.TryParse(one, out iOne);
Int32.TryParse(two, out iTwo);
return (iOne + iTwo).ToString();
}

EDIT:

As Alexei Levenkov stated you could/should handle exceptions.
Maybe something like this will help you during development

    private static int AddTwoStrings(string one, string two) 
{
int iOne = 0;
int iTwo = 0;
bool successParseOne = Int32.TryParse(one, out iOne);
bool successParseTwo = Int32.TryParse(two, out iTwo);
if (!successParseOne)
{
throw new ArgumentException("one");
}
else if(!successParseTwo)
{
throw new ArgumentException("two");
}

return (iOne + iTwo);
}

So when you have a wrong number you will be notified if you use try/catch

Adding two numbers concatenates them instead of calculating the sum

They are actually strings, not numbers. The easiest way to produce a number from a string is to prepend it with +:

var x = +y + +z;

How can I find the sum of two numbers which are in String variables?

Java provides parse methods for Primitive Types. So depending on your input you can use Integer.parseInt, Double.parseDouble or others.

String result;
try{
int value = Integer.parseInt(a)+Integer.parseInt(b);
result = String. valueOf(value) ;
}catch(NumberFormatException ex){
//either a or b is not a number
result = "Invalid input";
}
JOptionPane.showMessageDialog(null,result);

Swift - Add two string numbers

Try this:

    if let currentStamps = Int(labelStamps.text!), let stamps = Int(textFieldStamps.text!) {
labelStamps.text = "\(stamps + currentStamps)"
}

JavaScript adding a string to a number

What you are talking about is a unary plus. It is different than the plus that is used with string concatenation or addition.

If you want to use a unary plus to convert and have it added to the previous value, you need to double up on it.

> 3 + 4 + "5"
"75"
> 3 + 4 + +"5"
12

Edit:

You need to learn about order of operations:

+ and - have the same precedence and are associated to the left:

 > 4 - 3 + 5
(4 - 3) + 5
1 + 5
6

+ associating to the left again:

> 3 + 4 + "5"
(3 + 4) + "5"
7 + "5"
75

unary operators normally have stronger precedence than binary operators:

> 3 + 4 + +"5"
(3 + 4) + (+"5")
7 + (+"5")
7 + 5
12

javascript (+) sign concatenates instead of giving sum?

Here value gives you a string, hence the concatenation. Try parsing it as an Number instead:

var sum = parseInt(numberOne) + parseInt(numberTwo);

See the demo fiddle.



Related Topics



Leave a reply



Submit