对于文本框文本更改,计算不起作用 [英] Calculation is not working for Textbox text change

查看:114
本文介绍了对于文本框文本更改,计算不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含三个文本框的网络表单。 Textbox1,Textbox2和Textbox3。我在Textbox2上有一个文本框文本更改来进行计算,但它没有正确执行。 Textbox3将显示答案。我想要得到两个数字之间的百分比差异。这是我的代码:



  protected   void  TextBox2_TextChanged( object  sender,EventArgs e)
{
int a = Convert.ToInt32(TextBox1.Text);
int b = Convert.ToInt32(TextBox2.Text);
TextBox3.Text = Convert.ToString((a - b)/((a + b)/ 2 )* 100 );
}





我在代码中做错了什么不能正常工作?

解决方案

百分比差异是

(ABS(A和B之间的差异)/((A + B)/ 2))* 100 

你的代码会做什么...除了你的代码使用整数数学。

因为(A - B)总是小于(A + B)为正数字,你的代码总是会产生零。



将所有值更改为 float double 它会正常工作。


考虑到a和b是正数,那么(a - b)将始终小于(a + b) )。在整数运算中,较小的整数除以较大的整数(当然相互比较)将始终为零。因此,如果两个整数的差值小于总和的一半,则总是为零。



只需更改您的计算:



 TextBox3.Text = Convert.ToString((a  -  b)*  100  /(( a + b)/  2 )); 





甚至是这样的。



 TextBox3.Text = Convert.ToString((a  -  b)* 100.0 /((a + b)/ 2)); 





干杯!





我刚注意到你的计算基于TextChanged事件。这是不合适的,因为每次编辑都会触发此事件,并且您无法确保每个文本字段都已正确填充,并且您也没有检查转换是否正确完成。据我所知,它可能会根据您输入的内容引发异常。



 尝试 
{
int a = Convert.ToInt32(TextBox1.Text);
int b = Convert.ToInt32(TextBox2.Text);
TextBox3.Text = Convert.ToString((a - b)* 100 0 / ((a + b)/ 2 ));
}
catch ()
{
TextBox3.Text = 转换失败!;
}

这样会更合适。







[/编辑]


I have a web form with three textboxes on it. Textbox1, Textbox2 and Textbox3. I have a textbox text change on Textbox2 to do a calculation but it is not doing it right. Textbox3 will display the answer. I am trying to get the percentage difference between two numbers. Here is my code:

protected void TextBox2_TextChanged(object sender, EventArgs e)
        {
            int a = Convert.ToInt32(TextBox1.Text);
            int b = Convert.ToInt32(TextBox2.Text);
            TextBox3.Text = Convert.ToString((a - b) / ((a + b) / 2)* 100);
        }



What did I do wrong in the code for this not to work?

解决方案

Percentage difference is

(ABS(difference between A and B) / ((A + B) / 2)) * 100

Which is what your code does...except your code uses integer math.
And since (A - B) will always be smaller than (A + B) for positive numbers, your code will always generate zero.

Change all the values to float or double and it'll work fine.


Considering that a and b are positive numbers then (a - b) will always be less than (a + b). In integer arithmetic an smaller integer divided by a larger integer (compared to one another of course) will always be zero. So if the difference of both integers is smaller than half the sum you will always end up with zero.

Just change your calculation like this:

TextBox3.Text = Convert.ToString((a - b) * 100 / ((a + b) / 2));



or even like this.

TextBox3.Text = Convert.ToString((a - b) * 100.0 / ((a + b) / 2));



Cheers!

[Edit]
I just noticed that you based your calculation on the TextChanged event. That is not appropriate, as this event is being fired for every edit that is made and you're not making sure that every text field is properly filled and you're also not checking that the conversion is done properly. For all I know it may throw exceptions based on what exactly your input was.

    try
    {
        int a = Convert.ToInt32(TextBox1.Text);
        int b = Convert.ToInt32(TextBox2.Text);
        TextBox3.Text = Convert.ToString((a - b) * 100.0 / ((a + b) / 2));
    }
    catch()
    {
        TextBox3.Text = "Conversion failed!";
    }

This would be more appropriate.




[/Edit]


这篇关于对于文本框文本更改,计算不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆