I am having some difficulties getting a string converted to an int in JavaScript. Say I have some code like this:
var numberString "12345";
var numberInt;
numberInt = int.Parse(numberString);
numberInt++;
// numberInt now is 12346
Thats what should happen. I converted the C# code from here to that JS. Problem is, it seems like it doesn’t know that numberString is the right type of string or something, because it outputs the error “System.FormatException: Invalid format” on the int.Parse() line.
I assume that the missing = operator on line 1 is just a transcription error…
For some reason int.Parse (Or rather System.Int32.Parse) is defined to return System.Object instead of an int.
If you explicitly define the type of numberInt, it should work:
var numberInt: int;
… although the InvalidFormat exception is raised when you give Parse a string that can’t be parsed as a number, so I have a suspicion that you already were that far.
Thank you Joachim, but this is giving me some trouble in my array.
var currentInt = 0;
var currentStr = "0";
function Update () {
if (currentInt != 10){
var node = Array();
node.Add(currentInt);
node[currentInt] = GameObject.Find (currentStr);
currentInt ++;
currentStr = currentInt.ToString();
}
}
It appears the original variable goes through nicely, but once i add one and try to go through a second time it gives me “Array index is out of range.”
Is this properly converting into a string? If yes, did I miss something else?
I did not like the above ways of converting between int and float. I was unable to get int.TryParse(string,int) to work, System.Int32.TryParse(string,int) did not work, and while parseInt (which is completely undocumented from what I can tell) failed on strings which contained characters.
This is the method I used:
try
{
var myInt : int = System.Convert.ToInt32(levelToLoad);
}
catch (err)
{
//code to handle non-numeric strings here
}
For new comers, check out the .Net Framework documentation on System.Convert.
The reasons why they dont work is because you guys dont declare the variables… i see var i = “stuff” and things like that… it works better if you type var i:String = “stuff”; it’s also less intensive for unity and it’s easier to see whats what…
That’s completely wrong. var i = “stuff” is exactly the same as var i : String = “stuff”. It doesn’t work better or worse either way, nor is it any more or less intensive either way. It’s called “type inference”. I would also disagree that it’s any easier to see what’s what, at least in this case, since putting something in quotes makes it very obvious that it’s a string, especially with syntax coloring.
That’s what everyone thinks. Then they get some programming experience and learn how to actually program efficiently. You shouldn’t reply to programming questions based entirely on what you think is right. Programming requires perfect precision knowledge, and does not work well on feelings. Atleast not until someone makes a proper AI of some sort…
While you are correct that the end result is essentially the same between var i = “stuff”, and var i:String = “Stuff”, that will not hold true once your project turns resourcedemanding enough for you to want to savor every little bit of cpu-cycle you can on every nook and cranny to gain better fps.
var i:String = “Stuff”; // this one is Ever so slightly faster, and is in fact part of the optimization process you should do on every project.
Sure, simple projects won’t need as much attention to optimizations as larger ones, but it’s a good programming habit to always declare your types. Even when it’s not needed.