How to convert int to string in JavaScript?
if you have a variable like
var i : int = 5;
var s: string;
s = “”+ i;
javascript is an interpreted language and it will convert the integer directly into the correspondent string.
Hope this helps.
Use .ToString().
Javascript in Unity is compiled, not interpreted (as well as not really being Javascript), and in any case that doesn’t have anything to do with the conversion. It’s better to use ToString rather than string concatenation anyway.
–Eric
For the sake of my curiosity… what’s the difference?
It’s a little faster, has less memory allocation, and it’s better (more obvious) code.
–Eric
ToString( ) also gives you more options as to formatting when you need it as well.
like ToString(“f5”) will return a string equivalent of a float out to 5-decimal places. But yea essentially
“” + variable;
Allocates a string for the “”
Performs a ToString( ) on the variable (allocating another string)
then concatenates them
is it possible convert string to float or integer?
C#
atoi?
float.parse (“5”);
int.parse (“5”);
miss c
if you want to avoid exceptions for non parsable stuff:
var f : float;
if( float.TryParse( “0.2”, f ) ){
// f will be 0.2
}else{
// could not parse string f will be 0.0
}