-1

The code looks like this:

private void button1_Click(object sender, EventArgs e)
{            
    bet = int.Parse(textBox1.Text);                      
}

It works fine when the textbox contains whole numbers like 10, 5, 6, etc. But with decimals like 10,5 it always returns:

system.formatexception 'input string was not in a correct format.'

Any help is much appreciated!

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137

4 Answers4

1

decimal.Parse is best in your case

Rajanikant Hawaldar
  • 314
  • 1
  • 5
  • 12
0
bet = Convert.ToDecimal(textBox1.Text);
Yargicx
  • 1,704
  • 3
  • 16
  • 36
0

to convert a string to a decimal number you can use the method Parse() of decimal number types.

For example:

  • float.Parse("2.5");
  • double.Parse("5.6");
  • decimal.Parse("8.39");

These methods will return the number parsed as a decimal type with the main difference being the precision.

If you want to parse forcing the use of the comma, you can provide as the second parameter an IFormatProvider object with the needed locale.

For example: double.Format("45,892", new CultureInfo("it"));

0

Resolved.

doing decimal.Parse(); didn't work at first but then I realized that I had accidentally defined bet like this further up:

int bet;

I changed it to:

decimal bet;

It now works great, thanks for the answers!