How do I convert an int to a string, for example if a variable is declared as an int and it needs to take user input. I need to convert it so that the user can enter a value. Then I need to do some calulations on the value entered by the user. I know you have to user int 32 parse but I don't know how and where to put it
How to convert int to string
9 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Arturo Salvamante3 pointsPosted Oct 2, 2006, 11:11 PM EDT
Drew Taylor66 pointsPosted Oct 4, 2006, 2:18 AM EDT
It says The type or namespace num Could not be found. What does this mean?
jc mandapat16 pointsPosted Oct 8, 2006, 1:40 AM EDT
saravanan gajendran23 pointsPosted Oct 8, 2006, 10:01 PM EDT
mohitchawla4 pointsPosted Oct 9, 2006, 5:13 AM EDT
use any of the below mentioned methods:
int a = 10;
Convert.ToString(a);
or
int a = 10;
string str = a.ToString();
Solitaire85 pointsPosted Oct 9, 2006, 5:45 PM EDT
string sa; int ia;
Console.Write("Enter a number: ");
sa = Console.ReadLine();
Int32.TryParse(sa, out ia);
Venkata Sirish4 pointsPosted Oct 12, 2006, 4:39 AM EDT
Drew Taylor66 pointsPosted Oct 13, 2006, 12:21 AM EDT
Thanks for you replies everyone. I know how to convert data types, but I don't know how to use tryparse.
Solitaire85 pointsPosted Oct 14, 2006, 12:52 PM EDT
Type to convert to
dot
TryParse
(
the string variable
,
out
the variable of the type to convert to
)
;
Example for converting a string named sa to an integer named ia, supposing that the user input is placed in TextBox1:
string sa; int ia;
sa = TextBox1.Text
Int32.TryParse(sa, out ia);
Is that clear enough for you?
Also remember that if you want to process the number mathematically and display the result in another textbox, you need to convert the number back into a string again.
string snum; int inum;
snum = Convert.ToString(inum);
TextBox2.Text = snum