Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a code that should round to the nearest whole number and when I test it the number is not rounding. When I enter 1523 in TextBox1 that number is divided into 12 and the answer is 126.91 in TextBox2 and Rounded to the nearest whole number but it doesn't Round the number. 126 in displayed in TextBox2. What did I do wrong and what did I forget?

C#
protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            int i = Convert.ToInt32(TextBox1.Text.Replace(",", ""));
            int j = 12;
            TextBox2.Text = Convert.ToString(i / j);

            int a = Convert.ToInt32(TextBox3.Text.Replace(",", ""));
            int b = Convert.ToInt32(TextBox4.Text.Replace(",", ""));
            int c = Convert.ToInt32(TextBox2.Text.Replace(",", ""));
            int d = Convert.ToInt32(TextBox5.Text.Replace(",", ""));
            TextBox6.Text = Convert.ToString(a + b + c + d);

            int g = Convert.ToInt32(TextBox6.Text.Replace(",", ""));
            int f = Convert.ToInt32(TextBox7.Text.Replace(",", ""));
            TextBox8.Text = Convert.ToString(g + f);


            int o = Convert.ToInt32(TextBoxLY1.Text.Replace(",", ""));
            int p = Convert.ToInt32(TextBox8.Text.Replace(",", ""));
            TextBoxF40.Text = Convert.ToString(Math.Round((Math.Abs(p - o) * 100.0 / ((o)))));
            TextBoxF40.Text = Math.Round(Convert.ToDouble(TextBoxFTE40.Text), 2).ToString();

            RangeValidatorLY1.Validate();
            RangeValidatorLY2.Validate();
            TextBox2.Text = Math.Round(Convert.ToDouble(TextBox2.Text), 2).ToString();
            TextBox2.Text = string.Format("{0:0,0}", double.Parse(TextBox2.Text));
            TextBox1.Text = string.Format("{0:0,0}", double.Parse(TextBox1.Text));
            TextBox6.Text = string.Format("{0:0,0}", double.Parse(TextBox6.Text));
            TextBox8.Text = string.Format("{0:0,0}", double.Parse(TextBox8.Text));
            TextBox9.Focus();
        }
Posted
Updated 19-Nov-15 14:22pm
v3
Comments
Matt T Heffron 19-Nov-15 20:39pm    
Ok, I looks like you updated your question with more complete example, but there's no evidence that you incorporated any of the advice you received in either of the Solutions below.
Fix the issue of the integer division.
Change to smarter parsing of the strings to int.
Handle input errors (at least put in checks and then a TODO comment for the error case).
Check that the format strings are actually doing what you think when given a value that is a double. (Do they "round" or "truncate"??)
You set TextBox2.Text 3 times, only the last one matters! Save the intermediate values in int or double variables. It is much more efficient.

Several things are wrong with this.
First don't convert it to a string until you need to display it in the TextBox.
Then, you round it to 2 places and then format it with zero decimal places.
There are better choices for parsing the input string.
C#
using System.Globalization;

int i;
if (!int.TryParse(TextBox1.Text, NumberStyles.Integer | NumberStyles.AllowThousands, CultureInfo.CurrentCulture, out i))
{
  // parse failed, handle this as you think best
}
int j = 12;  // or declare this as double then the cast on the next line is unnecessary

double scaled = (double)i / j;

// this rounds to 2 decimal places!!!
scaled = Math.Round(scaled, 2);
// you probably want
scaled = Math.Round(scaled);

TextBox2.Text = string.Format("{0:0,0}", scaled);
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Nov-15 17:50pm    
Sure, a 5.
I would also question the need for rounding. It's not often really needed. Using just string formatting would do it. Rounding may be dangerous if its result accidentally get to some intermediate calculations.
—SA
Try
C#
int i = Convert.ToInt32(TextBox1.Text.Replace(",", ""));
TextBox2.Text = Convert.ToString(i / 12.0);


You should read C# documentation about division with integers.

C#
TextBox2.Text = Math.Round(Convert.ToDouble(TextBox2.Text), 2).ToString();
TextBox2.Text = string.Format("{0:0,0}", double.Parse(TextBox2.Text));

You look pretty complicated in your head.
Extensive C# documentation/tutorials reading will not harm.
 
Share this answer
 
Comments
Computer Wiz99 19-Nov-15 15:32pm    
I made the changes and it worked. Now I have an issue with Input string not in correct format with this code: int c = Convert.ToInt32(TextBox2.Text.Replace(",", ""));. Why is this happening?
Patrice T 19-Nov-15 15:52pm    
That is another question.
You need to give examples of strings that works and of strings that don't work.
The problem is in the string values.
Patrice T 19-Nov-15 18:25pm    
You should either update your question with the second issue or ask a new question for the second issue.

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