- Home /
Convert int to string and back again
Hi all, I'm having a problem after I convert one of my int's to a string..I can't get it to convert back to an int! Here's some sample code to show you what i'm dealing with.
var price : int;
function IntToString() {
price.ToString();
}
function StringToInt() {
price = System.Int32.TryParse(price); <<<ERROR HERE
}
I'm getting the error "No appropriate version of int.TryParse for the argument list '(int)' was found."
Can someone give me an idea what I'm doing wrong? Thank you!
Answer by Fabkins · Apr 28, 2012 at 11:37 AM
Here you go:
var nu1: int = 100;
var str: String ="";
var nu2: int;
// Convert int to string
str= nu1.ToString();
// Convert string to int
nu2=System.Int32.Parse(str);
Debug.Log(nu1);
Debug.Log(str);
Debug.Log(nu2);
Never fails... People mark the answer with a "here's a script for you" area in it ins$$anonymous$$d of another answer that was posted first, and said the same thing in it (or more) as the one that got accepted... lol...
Sorry didnt notice. Why didnt you just post as an answer rather than a comment?
Because I was only commenting... I meant it about @kolban answer being the one this person should have accepted. Nothin against you, was just talking sh#* about how people on here almost always accept the answer with the biggest code-chunk in it rather than anything else lol
Answer by kolban · Apr 28, 2012 at 04:25 AM
According to the documentation of TryParse, it wants two parameters, a string and an output int ... see:
http://docs.mono-android.net/?link=T%3aSystem.Int32%2fM%2fTryParse
Did you perhaps mean to use the Parse() method? See:
http://docs.mono-android.net/?link=T%3aSystem.Int32%2fM%2fParse
This however would also not work as in your code you are passing in "price" which is an int and both Parse() and TryParse() expect a String as input.
Have a look at this. price is always an int. It's not getting turned into a string permanently with ToString() and you don't need to turn it back into an int afterward, because it already is one.
var price : int = 10 ;
function Update(){
if(Input.Get$$anonymous$$ouseButtonDown(0))
StringToIntTest() ;
}
function StringToIntTest(){
var stringTest : String = price.ToString() ;
var stringTestAsInt : int = parseInt(stringTest) ;
Debug.Log("Price + 5 = " + (price + 5)) ;
price += stringTestAsInt ;
Debug.Log("Price + price as string and back into an int = " + price) ;
}
Ahh ok, i am needing to convert it to string so I can save it in a string array. Are you saying I don't need to do anything to convert it back to an int after the data is loaded?
EDIT: I just wanted to clarify because I have to do this for a lot of variables and don't want to have to go back and do it all over again.
Well, if you save it AS a string, then what you load will be a string. So, yes, you'll have to convert the loaded string into an int if you want to use it as an int; but that wasn't shown in your main post as being what you were trying to do.
Your answer
Follow this Question
Related Questions
Converting a string into int is rounding off 1 Answer
Download a WWW int? Or convert? 1 Answer
int to string, weird string value? 1 Answer
convert string to int 0 Answers
c# convert int to string 2 Answers