Haxe Int to String
Asked Answered
D

1

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?

Disseminule answered 8/2, 2013 at 17:25 Comment(0)
Z
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.

Zicarelli answered 8/2, 2013 at 19:44 Comment(2)
Would it also be possible to convert the string back into an integer?Ockham
Sure thing: var i = Std.parseInt(s);Zicarelli

© 2022 - 2024 — McMap. All rights reserved.