How do I add two strings in C #?

7

I have a method that will get two strings and return the sum of them in string format.

I use string since there will be +30 digits. I'm having trouble converting to add up. :

    
asked by anonymous 11.06.2015 / 22:05

2 answers

6

As you're talking about converting to ulong I'll start from the beginning that strings represent integers.

public string Somar(string numA, string numB)
{
    BigInteger bigA = BigInteger.Parse(numA);
    BigInteger bigB = BigInteger.Parse(numB);
    return BigInteger.Add(bigA, bigB).ToString();
}

If you want to treat float or double switch BigInteger with BigDecimal . An exception of type FormatException will be thrown if it can not parse .     

11.06.2015 / 22:18
3

If you are using .Net Framework 4.0 or higher you can use BigInteger . You will only need to reference the following System.Numerics assembly.

    
11.06.2015 / 22:14