简体   繁体   English

为什么我的 float 变量没有从文本框中获取正确的值?

[英]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 typetextbox的字符。

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.

相关问题 为什么我的float变量语句不断显示“无法将类型double隐式转换为float”错误? - why does my float variable statement keeps on giving me an 'Cannot implicitly convert type double to float' error? 为什么我的变量失去价值? - Why does my variable lose its value? 为什么我的保存使用TextBox的初始值而不是输入的值? - Why does my save use the initial value of my TextBox and not the entered value? Linq查询从文本框获取输入时不返回值 - Linq query in not returning value when taking input from TextBox 为什么在我的文本框中未更新Capacity属性? - Why does the Capacity property not update in my textbox? 为什么ShowDialog选择我的TextBox中的文本? - Why does ShowDialog select text in my TextBox? 为什么更新第一个变量值时第二个变量值会改变? - Why does my second variable value change when updating the first? 为什么在“剃刀”页面上无法获得正确的文本框值? - Why can't I get the correct textbox value on Razor page? 从文本框中获取输入并将其插入 mysql 数据库时,如何更正我的日期时间值? - how do I correct my date time value when taking an input from a text box and inserting it into mysql database? 从文本框中获取传递给我的控制器的变量 - get a variable passed to my controller from a textbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM