-1

The C# program receives a string labeled:

1.2345 V

I need to compare this value using < or > in a 'if' statement. How do I convert the string above to a integer? I tried to use:

int anInteger;
anInteger = Convert.ToInt32(textBox1.Text);
anInteger = int.Parse(textBox1.Text);

But it throws the error System.FormatException: incorrect format.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • 1
    It's _not_ an integer but a double/decimal value. You also have to remvoe the `V` before you can parse it – Tim Schmelter Sep 15 '17 at 13:29
  • what integer value do you expect out of `1.2345 V`? – Rahul Sep 15 '17 at 13:29
  • Do you only want the numbers or does the `V` have a special meaning because that string cannot be converted to a numeric value. – Jerodev Sep 15 '17 at 13:30
  • 1
    Possible duplicate of [How to convert string to integer in C#](https://stackoverflow.com/questions/2344411/how-to-convert-string-to-integer-in-c-sharp) – SᴇM Sep 15 '17 at 13:30
  • 2
    Possible duplicate of [How to Solve System.FormatException??](https://stackoverflow.com/questions/10702431/how-to-solve-system-formatexception) – EJoshuaS - Stand with Ukraine Oct 15 '17 at 13:53

4 Answers4

1

In case you insist on integer (dot in 1.2345 shoud be ignored and final result is 12345):

  // Any digits (including, say, Persian ones) are OK  
  int anInteger = (textBox1.Text
    .Where(c => char.IsDigit(c))
    .Aggregate(0, (s, a) => s * 10 + (int)char.GetNumericValue(a));   

Or

  // Only '0'..'9' digits supported
  int anInteger = (textBox1.Text
    .Where(c => c >= '0' && c <= '9')
    .Aggregate(0, (s, a) => s * 10 + a - '0');   
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

You have to remove the V at the end and use decimal.Parse/ TryParse:

decimal d;
bool validFormat = decimal.TryParse(textBox1.Text.TrimEnd('V', ' '), out d);

In my country which uses , as decimal and . as group separator this yields 12345.

If you instead want to ignore anything that is not a digit in a string:

int number = int.Parse(new string(textBox1.Text.Where(char.IsDigit).ToArray()));
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

Please also notice that depends on your current culture settings you can get different results.

Following code was run with de-DE culture settings

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("de-DE");

string str = "1.23";
decimal val = decimal.Parse(str);
val.Dump(); // output 123

string str2 = "1,23";
decimal val2 = decimal.Parse(str2);
val2.Dump(); // output 1,23

Following code was run with en-US culture settings

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

string str = "1.23";
decimal val = decimal.Parse(str);
val.Dump(); // output 1.23

string str2 = "1,23";
decimal val2 = decimal.Parse(str2);
val2.Dump(); // output 123

Please use LINQPad to run that code.

rraszewski
  • 1,135
  • 7
  • 21
0

you can try -

 decimal dec=2;
 string str = "3.23456";
 dec = Convert.ToDecimal(str.ToString());
 int a = Convert.ToInt32(dec);
Nofar Eliasi
  • 109
  • 5