JS, TS, AS3 all have built-in 'toFixed', why doesn't 'openfl' have this method?

.


02

.
JS, TS, AS3 all have built-in ‘toFixed’, why doesn’t ‘openfl’ have this method?
.

I can’t tell you the “why”, but here’s one way to accomplish rounding to two decimal places, in one line:

var num:Float = 1.23456789;
var s:String = Std.string(Math.round(num * 100) / 100); // "1.23"

If you want to round to something other than 2 decimal places, replace the 100) / 100 there. As a simple rule of thumb, the number of zeroes on the number (100 has two zeroes), is the number of decimal places you get.

So 10 (one zero) gives you one decimal place

var s:String = Std.string(Math.round(num * 10) / 10); // "1.2"

1000 (three zeroes) gives you three decimal places:

var s:String = Std.string(Math.round(num * 1000) / 1000); // "1.234"

…etc

Your method is not working, what I want is always two decimal places. 'toFixed ‘is done very well. Do you understand the implementation principle of’ toFixed '?

This:

var num:Float = 1.23456789;
var s:String = Std.string(Math.round(num * 100) / 100); // "1.23"

Takes a Float of 1.23456789 and converts it to a String of "1.23".

Perhaps you might discuss what you would expect it to do instead.

Sometimes your method only displays one decimal point, while I need to display two decimal points, even if both decimal points are 0

var num:Number = 4;
trace(num.toFixed(2)); // 4.00

You might consider raising this question in the Haxe community.