Hello,
Is there a best way to get Number.toFixed using Haxe? I can't find it in anywhere in the existing core API
Suggestions like the following, to multiply and divide by a precision factor, do not work for all numbers
var input:String = "2495.70";
var parsed:Float = Std.parseFloat(input);
trace("parsed:" + parsed);//2495.7
var product:Float = parsed * 100;//do something with our float
trace("product:" + product);//249569.99999999997, would expect 249570
var prec = 100;
trace("rounded:" + Std.int(product*prec) / prec );//249569.99
To get around this, I am just getting access to the "native" toFixed method in a roundabout way, but this will surely break on targets other than Flash and Javascript
public static function toFixed(x:Float, decimalPlaces:Int):String {
var f = Reflect.field(x, "toFixed");
return Reflect.callMethod(x, f, [decimalPlaces]);
}
Any ideas appreciated.
Thanks,
James