Click here to Skip to main content
15,901,284 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I thought I was rounding up correctly but looks like I set textBox4 value before I use it in the division for getting textBox5 value. The Roundup needs to be in the first part of the code ad I cant figure it out. Can anyone help?

C#
var roundedInput = Math.Round(tbTrailer_Needed, 2, MidpointRounding.AwayFromZero);

if (!String.IsNullOrEmpty(tbSnp_Day.Text) && !string.IsNullOrEmpty(cbSnp_Uld_Trl.Text))
    tbTrailer_Needed.Text  =  (Convert.ToInt32(tbSnp_Day.Text) / Convert.ToInt32(cbSnp_Uld_Trl.Text)).ToString();


What I have tried:

Math.Round, MidpointRounding mode
Posted
Updated 9-Nov-16 10:22am
v7
Comments
#realJSOP 7-Nov-16 18:20pm    
Check the source code you pasted. It looks like it got all jumbled up.
Member 12349103 7-Nov-16 18:39pm    
I have removed the Math.Ceiling from the second If statement and added it to the first and get an error textbox can not be used in this meathod
Philippe Mori 7-Nov-16 21:51pm    
Write readable code... Maybe you would understand it or someone might help you...
an0ther1 7-Nov-16 23:52pm    
Use your debugger, the values are not what you are actually expecting.
For instance - when you divide an int by an int you will always get an int (EG: 10/3 = 3, 9/3 = 3)
Math.Ceiling will round up, Math.Floor will round down & both require either a double or decimal value.

1 solution

First off, rounding only works with floating point values - trying to round integers is not going to work.
Second, dividing any number (other than zero) by itself will always give the same value: one. And the Ceiling value of one is one.
Third, do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...

Do your conversions first - and use TryParse instead, as it allows you to respond to user input errors instead of your application crashing - then use the converted values, and finally generate your outputs:
C#
double myValue;
if (string.IsNullOrWhitespace(myTextBox.Text) || !double.TryParse(myTextBox.Text, out myValue))
   {
   // Report problem to user
   return;
   }
double myOtherValue;
...
//Use myValue and myOtherValue, and myYetAnotherValue here and generate your results.
double myResult = ...
//And finally output.
myOutputTextBox.Text = myResult.ToString();
It's a lot clearer what is going on, and a whole lot easier to debug where a problem is.
 
Share this answer
 
Comments
OriginalGriff 9-Nov-16 2:41am    
Sorry? Do you want to try that again, but explaining in more detail - because that means little or nothing to me as a sentence...

Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work from.
OriginalGriff 9-Nov-16 10:17am    
So convert all of the values to a doubles using the code I gave you yesterday, and then round it up.
You can't just expect to pass a whole textbox to a Math method - it doesn't know what to do with it!
OriginalGriff 9-Nov-16 14:15pm    
Stop prefixing variables with things like "tb" until you know what it means and why some of older developers do it: in this case, it implies that the variable is a TextBox, which it clearly isn't.
Use IsNullOrWhiteSpace instead of IsNullOrEmpty - it does the same, but discards entries which are all spaces as well.
And then use TryParse instead of Convert - it checks for user errors instead of crashing your app.
Read what I wrote, instead of ignoring the code - or there really isn't any point in me typing it in the first place!
OriginalGriff 29-Nov-16 16:47pm    
No, it isn't:

double tbTrailer_Needed;

It's a double...
Member 12349103 2-Dec-16 16:18pm    
Which returns a value to textbox Trailer_Needed

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



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