Sunday, July 12, 2009

How to convert strings into numbers in C sharp?

I need to take a textBox1.Text number(int) value and a textBox2.Text number(int) value and display it in a label1 or textBox3. Please tell me how to either do this or tell me how to change a string value to an int value?





Thanks :\

How to convert strings into numbers in C sharp?
There are many ways actually.





int result = Convert.ToInt32 (TextBox1.Text);





and to convert to a fractional number.





double result = Convert.ToDouble (TextBox1.Text);





You should also check to see the string in the textbox is convertible before doing be convert or an exception will be thrown. You can However catch the exception in a try/catch block.
Reply:There is a Convert class so if you want to a string to a double





123.456 = Convert.ToDouble("123.456");


same for integers. ie


123 = Convert.ToInt32("123");


No comments:

Post a Comment