I recently had the task of summing together two very large numbers, possibly larger than int32’s.
My approach was first to:
try and parse the number as an int32 using int.Parse(), and return the value if successful.
If it didn’t parse, try and parse the number as a long using long.Parse(), and return the value if successful.
And if that didn’t parse, try and parse the number as an BigInteger, and return the value if successful using BigInteger.Parse()
Things i learned along the way:
- You need to add a reference to System.Numerics to the project
- When adding two BigIntegers together, you must use BigInteger.Add() as using number1 + number 2 returns -2
min int32 value: -2147483647
max int32 value: 2147483647
min long value: -9223372036854775808
max long value: 9223372036854775807