计算百分比公式不起作用 [英] Calculate percentage formula is not working
本文介绍了计算百分比公式不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表格会显示两个号码的百分比。当用户输入一个数字40,然后下一个数字是30时,计算完成,答案应为33%。当用户输入数字时,答案为-60。为什么会这样?我在公式中忘记了什么?
I have a form that will show what the percentage of two number. When a user enters a number say 40 and then the next number is 30, the calculation is done and the answer should be 33%. When the user enters the numbers, the answer comes to be -60. Why is this happening? What did I forget in my formula?
protected void TextBox3_TextChanged(object sender, EventArgs e)
{
int a = Convert.ToInt32(TextBox2.Text);
int b = Convert.ToInt32(TextBox3.Text);
TextBox4.Text = Convert.ToString(a - b / b * 100);
}
The percentage increase from 30 to 40 is:
(40-30)/30 * 100 = 33%
推荐答案
这是由于你没有添加括号
it is due to you have not add brackates
TextBox4.Text = Convert.ToString((a - b) / (b*1.0) * 100);
快乐编码! :)
Happy Coding! :)
比较你的例子和代码。但你应该在C#中研究评估的优先顺序和顺序。
见
http://msdn.microsoft.com/en-us/library/2bxt6kc4.aspx [ ^ ]
Compare your'e example and code. But you should study the precedence and order of evaluation in C#
See
http://msdn.microsoft.com/en-us/library/2bxt6kc4.aspx[^]
除解决方案1-2:don使用转换
,使用Parse
或TryParse
,以及ToString
(可能带有格式)。明确使用double type(或float)来计算百分比。
-SA
In addition to Solutions 1-2: don't useConvert
, useParse
orTryParse
, andToString
(possibly with format). Explicitly use double type (or float) to calculate percentage.
—SA
这篇关于计算百分比公式不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文