Click here to Skip to main content
14,741,362 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have this code that gives me the increase between two numbers.
int k = Convert.ToInt32(TextBoxLYTHCAS.Text.Replace(",", ""));
        int l = Convert.ToInt32(TextBoxTHCAS.Text.Replace(",", ""));
        TextBoxHC50.Text = Convert.ToString(Math.Round((Math.Abs(l - k)) * 100.0 / ((k))));
        TextBoxHC50.Text = Math.Round(Convert.ToDouble(TextBoxHC50.Text), 2).ToString();

It works fine until I entered in some numbers the other way around. I will show you first the correct way it works.

TextBoxLYTHCAS = 321
TextBoxTHCAS = 321
TextBoxHC50 = 93

As you can see this is not an increase between two numbers. It is equal to the same number. How can I just get the increase number to display in TextBoxHC50 and not show the decrease value in TextBoxHC50?
Posted
Comments
Sergey Alexandrovich Kryukov 13-Nov-14 16:29pm
   
Not clear what "increase between two numbers" should mean. Don't use convert. Don't use Round (why?)...
—SA
Computer Wiz99 13-Nov-14 20:58pm
   
I'm sorry I don't follow you.
Sergey Alexandrovich Kryukov 13-Nov-14 22:53pm
   
Please, you clarify first, and than I'll answer...
—SA
Kiran Wilson 13-Nov-14 23:17pm
   
I am getting 0 in TextBoxHC50, while entering 321 in TextBoxLYTHCAS and TextBoxTHCAS. Can you please clarify your example..
Computer Wiz99 14-Nov-14 5:53am
   
Okay, now can you do the same thing and put 4461 into TextBooxLYTHCAS and put 321 in TextBoxTHCAS. You will get 93. 93 is a decrease percentage from 4461. How can I correct this error? I know the calculations is doing what it is suppose to do but if a value that is entered into TextBoxTHCAS is less than the value in TextBoxLYTHCAS do not calculate. Is that possible?
johannesnestler 17-Nov-14 9:19am
   
I don't get you problem. Anyway doing
* reading from UI controls (without validation of course),
* modify the Input (replace...)
* Converting the Input
* applying your algorithm
* writing back to a UI-control (while converting...)

in one line of code is complete nonsense - stop this kind of programming and problems like yours where you mix up your "calculation" with other things and don't know where the problem comes from should dissapear - or at least give a clear hint what's wrong. E.g. create a function for you "algorithm" read input and validate it's in the right "order" - if not just change direction (or whatever is needed)...

1 solution

If I understand your question correctly, then you want to show the difference in third text box only when value of second text box is greater than first text box and don't want to show the result if case is reverse (second text box value < first text box value)

here is the code -
int k = Convert.ToInt32(TextBoxLYTHCAS.Text.Replace(",", ""));
           int l = Convert.ToInt32(TextBoxTHCAS.Text.Replace(",", ""));
           if (l >= k)
           {
               TextBoxHC50.Text = Convert.ToString(Math.Round((Math.Abs(l - k)) * 100.0 / ((k))));
               TextBoxHC50.Text = Math.Round(Convert.ToDouble(TextBoxHC50.Text), 2).ToString();
           }
           else
           {
               TextBoxHC50.Text = "";
           }
   
Comments
Dave Kreskowiak 7-Feb-16 0:09am
   
This question is a year and a half old. I doubt whoever it is is looking for the answer now.

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