简体   繁体   中英

Why does my float variable is not taking the correct value from the textbox?

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.

结果

 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);

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?

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM