Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have two textboxes on a web form. Thextbox1 has a textbox text change and textbox2 shows the answer. I am trying to get textbx2 to round to the nearest whole number on textbox text change. I am having an error on the rounding code line. Here is the code behind:

C#
protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        int w = Convert.ToInt32(TextBox1.Text.Replace(",", ""));
        int v = 0;
        TextBox2.Text = Convert.ToString(w + v);
        RangeValidatorLY1.Validate();
        TextBox2.Text = Math.Round(Convert.ToString(TextBox2.Text), 2);
        TextBox2.Text = string.Format("{0:0,0}", double.Parse(TextBox2.Text));
        TextBox3.Focus();

    }


What am I missing?
Posted
Updated 18-Sep-18 20:33pm
Comments
OriginalGriff 16-Oct-14 14:42pm    
"What am I missing?"

Dunno. But then, I can't see your screen, or work out what you entered...

What did it do that you didn't expect, or not do that you did?
Computer Wiz99 16-Oct-14 14:52pm    
on the line: TextBox2.Text = Math.Round(Convert.ToString(TextBox2.Text), 2);,
Math.Round(Convert.ToString(TextBox2.Text), 2); has a red line under it. Error saying: The best overloaded method match for 'System.Math.Round(decimal)' has some invalid arguments. And the second error says: Argument 1: cannot convert from 'string' to 'decimal'. I am just trying to round to the nearest whole number.

[no name] 16-Oct-14 14:51pm    
Without having a look to the help of Math.Round: But I really don't think, that Math.Round expects a String for the first paramet.... (?)

"on the line: TextBox2.Text = Math.Round(Convert.ToString(TextBox2.Text), 2);,
Math.Round(Convert.ToString(TextBox2.Text), 2); has a red line under it. Error saying: The best overloaded method match for 'System.Math.Round(decimal)' has some invalid arguments. And the second error says: Argument 1: cannot convert from 'string' to 'decimal'. I am just trying to round to the nearest whole number."



Um. Are you in the habit of converting strings into other strings? :laugh:
Possibly, what you meant to say was:
C#
TextBox2.Text = Math.Round(Convert.ToDouble(TextBox2.Text), 2).ToString();


But... since you add two integers to produce the string value in the first place, rounding is (by definition) not going to make any difference at all... :laugh:

Sorry, but that code as a whole doesn't make a lot of sense. What are you actually trying to do?
 
Share this answer
 
Comments
Computer Wiz99 16-Oct-14 15:13pm    
OriginalGriff, Thanks for the code. I was just trying to find a shorter way of coding for rounding numbers to the nearest whole number without using Boolean. You are right I do have a habit of converting strings into other strings. I just have a weird idea in my head. Just testing it out to see what I can come up with. Being on here has helped me to retract and redo some coding and make it better. My wife said that I am like a code scientist. :laugh:
Apparently, you don't need rounding, but you just need presenting rounded value in a text box. This is not the same. Explicit rounding is quite rarely needed and is potentially dangerous, because you can, be any chance, use rounded value as intermediate result which can cause loss of accuracy.

What you want should be resolved using proper string formatting. Please see:
http://msdn.microsoft.com/en-us/library/system.double.tostring%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/26etazsy%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8%28v=vs.110%29.aspx[^].

However, one reasonable case to use Math.Round would be getting an integer value from floating-point one, but I'm not sure this is what you need in your case.

Besides, it's bad to use Convert for your purpose. This is parsing, so it's better to use double.TryParse, which doesn't throw exception but return Boolean success result. (You can use double.Parse, too, but be ready to handle exceptions; invalid double value can always be entered in a text box, no matter how hard you filter the input).

—SA
 
Share this answer
 
v2
//Declare ceilingvalue variable
double ceilingvalue = Math.Ceiling(Convert.ToDouble(TextBox1.Text));
Label1.Text = ceilingvalue.ToString("#");
 
Share this answer
 
v2
Comments
CHill60 19-Sep-18 7:37am    
I strongly advise you to not use Convert.Totype() functions when dealing with user input from textboxes - you have no guarantee that the input is going to be in the correct format. Use the type.Parse() or type.TryParse() methods instead

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