[英]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.我需要计算百分比,但是当我在文本框中放置一个浮点值时,变量将该值转换为一个 int 值。
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 "."
如果我没记错的话,对于这个问题,也许你的 UI 框架(DevExpress)或 windows 表单组件无法识别"."
character in the textbox
when you're trying to convert(.Parse)
or validate it to float type
.当您尝试convert(.Parse)
或将其验证为float type
时textbox
的字符。
total = ((5000* 05)/100);总计 = ((5000* 05)/100); Debug: 250调试:250
And why you didn't try to put intereses
in MessageBox.Show("El "+textBox1.Text+" % de "+monto+" es "+ total);
以及为什么您不尝试将intereses
放在MessageBox.Show("El "+textBox1.Text+" % de "+monto+" es "+ total);
instead of textBox1.Text
to see what's the exact problem?而不是textBox1.Text
来查看确切的问题是什么?
Use the built in NumericUpDown
control and set the number of decimal places in it as shown below.使用内置的NumericUpDown
控件并在其中设置小数位数,如下所示。 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此控件旨在处理带或不带小数点的数值,它会为您处理小数点,因此您不必担心
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.