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

C#
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);
       }


SQL
The percentage increase from 30 to 40 is:
  (40-30)/30 * 100 = 33%
Posted
Comments
PIEBALDconsult 22-Sep-14 11:17am     CRLF
:cough: Order of operations :cough: And please don't use Convert, use Int32.TryParse instead. Better yet, use a NumericUpDown rather than a TextBox.
Computer Wiz99 22-Sep-14 11:20am     CRLF
Okay. I was just testing the formula to see what I did wrong.

it is due to you have not add brackates
C#
TextBox4.Text = Convert.ToString((a - b) / (b*1.0) * 100);

Happy Coding! :)
 
Share this answer
 
v2
Comments
Computer Wiz99 22-Sep-14 11:17am    
Aarti Meswania, I have tried this and the answer came to be 0.
PIEBALDconsult 22-Sep-14 11:19am    
Then step through with a debugger to see what actual values are being used.
Aarti Meswania 22-Sep-14 11:20am     CRLF
values should be float
Aarti Meswania 22-Sep-14 11:22am    
please check updated solution
Sergey Alexandrovich Kryukov 22-Sep-14 11:52am     CRLF
5ed. I added my 2 cents in Solution 3. —SA
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[^]
 
Share this answer
 
Comments
Computer Wiz99 22-Sep-14 11:33am    
Thanks for the link. This will help out a lot.
Sergey Alexandrovich Kryukov 22-Sep-14 11:52am     CRLF
5ed. I added my 2 cents in Solution 3. —SA
In addition to Solutions 1-2: don't use Convert, use Parse or TryParse, and ToString (possibly with format). Explicitly use double type (or float) to calculate percentage.
—SA
 
Share this answer
 
Comments
Aarti Meswania 22-Sep-14 12:02pm     CRLF
5+! :) TryParse and double type will make code perfect
Sergey Alexandrovich Kryukov 22-Sep-14 12:10pm     CRLF
Thank you very much, Aarty, but "perfect" would be a great exaggeration. :-) —SA
Aarti Meswania 22-Sep-14 12:14pm     CRLF
welcome! it isn't exaggeration. It's really perfect solution :)
Sergey Alexandrovich Kryukov 22-Sep-14 12:36pm     CRLF
Do you want me to point out other problems? :-) —SA
Sergey Alexandrovich Kryukov 22-Sep-14 12:37pm     CRLF
Do you want me to point out other problems? :-) I mean the code which would result in implementing our suggestions minimally? —SA

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