1

I need to convert a string to integer. My string can be of any type (float/int/string/special character).

For example:

If my string is "2.3", I need to convert to = 2
If my string is "anyCharacter", I need to convert to = 0
If my string is "2", I need to convert to = 2

I tried the following:

string a = "1.25";int b = Convert.ToInt32(a);    

I got the error:

Input string was not in a correct format

How do I convert it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

8 Answers8

3

Use Double.TryParse() and once you get the value from it, convert it to int using Convert.ToInt():

double parsedNum;
if (Double.TryParse(YourString, out parsedNum) {
  newInt = Convert.ToInt32(num);
} 
else {
  newInt = 0;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
K D
  • 5,889
  • 1
  • 23
  • 35
1

As far as I know, there isn't any generic conversion, so you'd have to do a switch to find out the type of the variable and then use either of the following (for each type):

int.Parse(string)

or

int.TryParse(string, out int)

The second one will return a boolean which you can use to see if the conversion passed or failed.

Your best option would be to use double or decimal parsing as this won't remove any decimal places, unlike int.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sayse
  • 42,633
  • 14
  • 77
  • 146
1

I think Convert.ToInt32 is the wrong place to look for - I would use Integer.Tryparse and if TryParse evaluates to false, assign a 0 to the variable. Before the TryParse, you could simply delete any character after the dot, if you find it in the string.

Also, keep in mind that some languages use "," as a separator.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Christian Sauer
  • 10,351
  • 10
  • 53
  • 85
1

Try:

if (int.TryParse(string, out int)) {
    variable = int.Parse(string);
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
samuraiseoul
  • 2,888
  • 9
  • 45
  • 65
1

Try to parse it as a floating point number, and convert to integer after that:

double num;
if (Double.TryParse(a, out num) {
  b = (int)num;
} else {
  b = 0;
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

This should help: treat any string as if it were a double, then Math.Floor() it to round it down to the nearest integer.

double theNum = 0;
string theString = "whatever"; // "2.3"; // "2";
if(double.TryParse(theString, out theNum) == false) theNum = 0; 
//finally, cut the decimal part
int finalNum = (int)Math.Floor(theNum);

NOTE: the if might not be needed per-se, due to theNum initialization, but it's more readable this way.

Alex
  • 23,004
  • 4
  • 39
  • 73
0

bool Int32.TryParse(string, out int)

The boolean return value indicates if the conversion was successful or not.

Srikanth Venugopalan
  • 9,011
  • 3
  • 36
  • 76
0

Try something like this:

public int ForceToInt(string input)
{
   int value; //Default is zero 
   int.TryParse(str, out value);

   return value;
}

This will do the trick. However I don't recommend taking this approach. It is better to control your input whereever you get it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jens Kloster
  • 11,099
  • 5
  • 40
  • 54