Home > OS >  ToFixed function on ActionScript/Javascript
ToFixed function on ActionScript/Javascript

Time:09-17

First time in stackoverflow, but I really need help on reconstructing this string.

So basically its in Actionscript and I'd need to reconstruct the Millions-string to output as 1.23M, AKA contain millions with thousands beside it as currently it only shows 1M. I have heard that toFixed would do the trick, but I can't seem to get it to work as my favour.

Any examples would help, thank you!

        public static function balanceToString(value:int):String
        {
            var suffix:String = "";
            var resultValue:int = value;
            if (value >= 1000000)
            {
                resultValue = Math.floor(resultValue / 1000000);
                resultValue.toFixed(4);
                suffix = "M";
            }
            else if (value >= 100000)
            {
                resultValue = Math.floor(resultValue / 1000);
                suffix = "K";
            }

            return ""   resultValue.toString()   suffix;
        }

CodePudding user response:

You are converting your number to int in the signature.

Try using a Number instead.

    public static function balanceToString(value:Number):String
    {
        var suffix:String = "";
        var resultValue:Number = value;
        if (value >= 1000000)
        {
            resultValue = Math.floor(resultValue / 1000000);
            resultValue.toFixed(4);
            suffix = "M";
        }
        else if (value >= 100000)
        {
            resultValue = Math.floor(resultValue / 1000);
            suffix = "K";
        }

        return ""   resultValue.toString()   suffix;
    }

CodePudding user response:

Something like this, I guess.

Implementation:

public static function balanceToString(value:int):String
{
    var suffix:String = "";
    var divisor:Number = 1;
    var precision:Number = 0;
    
    if (value >= 100000)
    {
        // This will display 123456 as 0.12M as well.
        divisor = 1000000;
        precision = 2;
        suffix = "M";
    }
    else if (value >= 500)
    {
        // This will display 543 as 0.5K.
        divisor = 1000;
        precision = 1;
        suffix = "K";
    }
    
    // This allows you to control, how many digits to display after
    // the dot . separator with regard to how big the actual number is.
    precision = Math.round(Math.log(divisor / value) / Math.LN10)   precision;
    precision = Math.min(2, Math.max(0, precision));
    
    // This is the proper use of .toFixed(...) method.
    return (value / divisor).toFixed(precision)   suffix;
}

Usage:

trace(balanceToString(12));         // 12
trace(balanceToString(123));        // 123
trace(balanceToString(543));        // 0.5K
trace(balanceToString(567));        // 0.6K
trace(balanceToString(1234));       // 1.2K
trace(balanceToString(12345));      // 12K
trace(balanceToString(123456));     // 0.12M
trace(balanceToString(1234567));    // 1.23M
trace(balanceToString(12345678));   // 12.3M
trace(balanceToString(123456789));  // 123M
  • Related