如何消除我的计算结果 [英] How to get the negative out of my calculation
本文介绍了如何消除我的计算结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有三个文本框可以对文本框文本更改进行计算。计算完成后,答案中有否定。这是我的代码:
I have three textboxes that do calculations on a textbox text change. When the calculations is done there is a negative in the answer. Here is my code:
int k = Convert.ToInt32(TextBox1.Text);
int l = Convert.ToInt32(TextBox2.Text.Replace(",", ""));
TextBox3.Text = Convert.ToString((k - l) * 100.0 / ((k + l) / 2));
TextBox3.Text = Math.Round(Convert.ToDouble(TextBox3.Text), 2).ToString();
这是我想做的事。
Here is what I am trying to do.
Calculate percentage difference
between V1 = 100 and V2 = 1000
( | V1 - V2 | / ((V1 + V2)/2) ) * 100
= ( | 100 - 1000 | / ((100 + 1000)/2) ) * 100
= ( | -900 | / (1100/2) ) * 100
= ( 900 / 550 ) * 100
= 1.636364 * 100
= 163.6364% difference
It is from this website:
<a href="http://www.calculatorsoup.com/calculators/algebra/percent-difference-calculator.php">http://www.calculatorsoup.com/calculators/algebra/percent-difference-calculator.php</a>[<a href="http://www.calculatorsoup.com/calculators/algebra/percent-difference-calculator.php" target="_blank" title="New Window">^</a>]
How will I get the negative out?
推荐答案
当你看到公式:
百分比差异=(| ΔV| /(ΣV/ 2))* 100 =(|(V1 - V2)| /((V1 + V2)/ 2))* 100
您知道必须使用绝对值( http://en.wikipedia.org/wiki/Absolute_value [ ^ ])差异V1-V2因此:
When you see the formula:
Percent difference = ( | ΔV |/ ( ∑V/2) ) * 100 = ( | (V1 - V2) | / ((V1 + V2)/2) ) * 100
You know you must use the absolute value (http://en.wikipedia.org/wiki/Absolute_value[^]) of the difference V1-V2 thus:
TextBox3.Text = Convert.ToString((Math.Abs(k - l)) * 100.0 / ((k + l) / 2));
这将使差价始终为非负值。
要回合:
This will make the difference always non-negative.
To round:
TextBox3.Text = Convert.ToString(Math.Round((Math.Abs(k - l)) * 100.0 / ((k + l) / 2)));
这篇关于如何消除我的计算结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文