Haxe Int to String

dunstantom picture dunstantom · Feb 8, 2013 · Viewed 8.2k times · Source

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?

Answer

Franco Ponticelli picture Franco Ponticelli · Feb 8, 2013

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.