Reputation: 143
How can I convert a string like "09335887170" to an integer? Here is what I have tried:
string a = "09335887170";
int myInt;
bool isValid = int.TryParse(a, out myInt); // it returns false in this case
if (isValid)
{
int plusOne = myInt + 1;
MessageBox.Show(plusOne.ToString());
}
MessageBox.Show(a);
int stringToInt = Convert.ToInt32("09335887170"); // it returns nothing with no error
MessageBox.Show((stringToInt + 1).ToString());
int test = int.Parse(a); //it has type convertion error
MessageBox.Show((test + 1).ToString());
Upvotes: 1
Views: 121
Reputation: 312
result = Convert.ToInt64(value);
Look here for more info.
(Found online, im not really in c#)
Upvotes: 3
Reputation: 385
The maximum value of a Int32
(or only int
) is 2,147,483,647
you need a UInt64
or Int64
or Long
string a = "09335887170";
Int64 myInt;
bool isValid = Int64.TryParse(a, out myInt);
if (isValid)
{
int plusOne = myInt + 1;
MessageBox.Show(plusOne.ToString());
}
Upvotes: 9