1

I need to calculate the percent but when I put a float value in the textbox the variable convert the value to an int value.

Results

 float intereses = 0, monto = 0, total = 0;

 intereses = float.Parse(textBox1.Text);
 monto = float.Parse(txtMonto.Text);

 total = ((monto * intereses)/100);

 MessageBox.Show("El "+textBox1.Text+" % de "+monto+" es "+ total);
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • 1
    [Check decimal separator](https://stackoverflow.com/questions/14513468/detect-decimal-separator) and make sure it is set to a '.', currently it is (probably) set to a ',' that's why `0.5` is not being parsed correctly – Luuk Oct 08 '22 at 15:35
  • What UI framework are you using? – Julian Oct 08 '22 at 15:36
  • I´m using Visual Studio 2013 and DevExpress – HenryDiaz1202 Oct 08 '22 at 15:42
  • 1
    see, for an example which should be helpful: https://onlinegdb.com/-H1NY2X_3, click on `Run`, and check the results..... – Luuk Oct 08 '22 at 15:52

2 Answers2

1

First, that's not necessary initialize variables in the first line.

If i'm not wrong, For the problem, maybe your UI framework(DevExpress) or windows form component cannot recognize "." character in the textbox when you're trying to convert(.Parse) or validate it to float type.

total = ((5000* 05)/100); Debug: 250

And why you didn't try to put intereses in MessageBox.Show("El "+textBox1.Text+" % de "+monto+" es "+ total); instead of textBox1.Text to see what's the exact problem?

Epic Soul
  • 11
  • 3
0

Use the built in NumericUpDown control and set the number of decimal places in it as shown below. This control is designed to handle numeric values with or without decimal point, it will handle the decimal point for you so that you don't have to worry about it

enter image description here

Siraf
  • 1,133
  • 9
  • 24