Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haxe Int to String

Tags:

string

haxe

It seems AS3 has a toString() for the Number class. Is there an equivalent in Haxe? The only solution I could come up with for converting an Int to a String is a function like:

public function IntToString(i:Int):String {
    var strbuf:StringBuf = new StringBuf();
    strbuf.add(i);
    return strbuf.toString();
}

Is there a better method that I'm overlooking?

like image 886
dunstantom Avatar asked Feb 08 '13 17:02

dunstantom


1 Answers

You don't usually need to manually convert an int to a string because the conversion is automatic.

var i = 1;
var s = "" + i; // s is now "1"

The "formal" way to convert any value to a string is to use Std.string():

var s = Std.string(i);

You could also use string interpolation:

var s = '$i';

The function your wrote is fine but definitely overkilling.

like image 115
Franco Ponticelli Avatar answered Oct 21 '22 04:10

Franco Ponticelli