Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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:

C#
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?
Posted
Updated 20-Oct-14 5:34am
v2
Comments
PIEBALDconsult 20-Oct-14 11:36am    
Try using floating point numbers rather than integers.

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.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 20-Oct-14 11:47am    
Which is exactly what I said. Anything new in this solution?
OriginalGriff 20-Oct-14 12:04pm    
You hadn't posted it when I started typing! :laugh:
Computer Wiz99 20-Oct-14 12:05pm    
First thing is that the link I showed you was what I was looking for. If you enter in two numbers on that link you will get back the same answer. Not a negative. Check it out:

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

And if I do it like this:

Calculate percentage difference
between V1 = 1000 and V2 = 100

( | V1 - V2 | / ((V1 + V2)/2) ) * 100

= ( | 1000 - 100 | / ((1000 + 100)/2) ) * 100
= ( | 900 | / (1100/2) ) * 100
= ( 900 / 550 ) * 100
= 1.636364 * 100

= 163.6364% difference

Same answer with no negative.

OriginalGriff 20-Oct-14 12:14pm    
Well yes - but your code doesn't take the absolute value of the difference, so any negative you get (and 100 - 1000 will give you a negative) will (rightly) be propagated to the final value.
Use http://msdn.microsoft.com/en-us/library/system.math.abs(v=vs.110).aspx on the result of the subtraction and you will lose the negative throughout.
Manfred Rudolf Bihy 20-Oct-14 11:49am    
Also note that OP is using the TextChanged event.
So there are likely to be some conversion exceptions.
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:

C#
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.

C#
    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]
 
Share this answer
 
v3
Comments
Computer Wiz99 20-Oct-14 11:49am    
Manfred R. Bihy, Thanks for the code but there is a problem with the second one. Plus I want the code to give the difference between tow numbers no matter how they are entered. Here is a link of what I am trying to do:

http://www.calculatorsoup.com/calculators/algebra/percent-difference-calculator.php
Manfred Rudolf Bihy 20-Oct-14 11:55am    
1. What exactly is the problem with the second one?
2. What do you mean by " no matter how they are entered."?
3. "Plus I want the code to give the difference between tow numbers" that goes contrary to what you posted in your question. There you said you wanted the percentage of the difference and not just the difference.

You need to be more precise.
Computer Wiz99 20-Oct-14 12:06pm    
First thing is that the link I showed you was what I was looking for. If you enter in two numbers on that link you will get back the same answer. Not a negative. Check it out:

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

And if I do it like this:

Calculate percentage difference
between V1 = 1000 and V2 = 100

( | V1 - V2 | / ((V1 + V2)/2) ) * 100

= ( | 1000 - 100 | / ((1000 + 100)/2) ) * 100
= ( | 900 | / (1100/2) ) * 100
= ( 900 / 550 ) * 100
= 1.636364 * 100

= 163.6364% difference

Same answer with no negative.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900