12

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?

Gama11
  • 31,714
  • 9
  • 78
  • 100
dunstantom
  • 551
  • 2
  • 5
  • 10

1 Answers1

22

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.

Gama11
  • 31,714
  • 9
  • 78
  • 100
Franco Ponticelli
  • 4,430
  • 1
  • 21
  • 24