AS3-Flash cs6 How to make numbers commas?

I make a game, when you click on a monster, your score gets +1. But when your count is over 1000, I would like it to be like 1000, not 1000. I'm not sure how to do this since I haven't learned much of the script. I am inserting number and punctuation into the font. Here is my code:

var score:Number = 0;

Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;

Monster.addEventListener(TouchEvent.TOUCH_TAP, fl_TapHandler);

function fl_TapHandler(event:TouchEvent):void
{
    score = score + 1;
    Taps_txt.text = (score).toString();
}

      

Help would be greatly appreciated.

+3


source to share


4 answers


To show score

the semicolon, you can do the following: (comments are inside the code)

var score:Number = 0;
var score_str:String;
var score_str_len:int;

Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;

Monster.addEventListener(TouchEvent.TOUCH_TAP, fl_TapHandler);

function fl_TapHandler(event:TouchEvent):void
{
    score = score + 1;

    score_str = score.toString();
    score_str_len = score_str.length;

    // here you can use score > 999 instead of score_str_len > 3
    Taps_txt.text = 
        score_str_len > 3 

        // example : 1780
        // score_str_len                        = 4
        // score_str.substr(0, 4 - 3)           = 1     : get thousands
        // score_str.substr(4 - 3)              = 780   : get the rest of our number : hundreds, tens and units
        // => 1 + ',' + 780                     = 1,780 : concatenate thousands + comma + (hundreds, tens and units)
        ? score_str.substr(0, score_str_len-3) + ',' + score_str.substr(score_str_len-3) 
        : score_str
    ;
    // gives :
    // score == 300   => 300
    // score == 1285  => 1,285
    // score == 87903 => 87,903

}

      

EDIT:



To support numbers greater than 999.999, you can do the following:

function fl_TapHandler(event:MouseEvent):void
{
    score = score + 1;
    score_str = score.toString();   
    Taps_txt.text = add_commas(score_str);  
}

function add_commas(nb_str:String):String {

    var tmp_str:String = '';    
    nb_str = nb_str.split('').reverse().join('');

    for(var i = 0; i < nb_str.length; i++){
        if(i > 0 && i % 3 == 0) tmp_str += ',';
        tmp_str += nb_str.charAt(i);
    }

    return tmp_str.split('').reverse().join('');

    /*

        gives : 

            1234        => 1,234
            12345       => 12,345
            123456      => 123,456
            1234567     => 1,234,567
            12345678    => 12,345,678
            123456789   => 123,456,789
            1234567890  => 1,234,567,890

    */
}

      

Hope this can help you.

0


source


This may not be the most elegant approach, but I wrote a function that will return a comma formatted string;

public function formatNum(str:String):String {
        var strArray:Array = str.split("");

        if (strArray.length >= 4) {
            var count:uint = 0;
            for (var i:uint = strArray.length; i > 0; i--) {
                if (count == 3) {
                    strArray.splice(i, 0, ",");
                    count = 0;
                }
                count++;

            }
            return strArray.join("");
        }
        else {
            return str;
        }
    }

      

I've tested it on some fairly large amounts and it seems to work fine. There is no upper limit on the size of the number, so

trace (formatNum("10000000000000000000"));

      

The output will be:



10.000.000.000.000.000.000

So, in your example, you can use it this way;

Taps_txt.text = formatNum(String(score));

      

(This implicitly uses the type toString();

, but clearly doesn't work)

+1


source


You can do this:

function affScore(n:Number, d:int):String {
    return n.toFixed(d).replace(/(\d)(?=(\d{3})+\b)/g,'$1,');
}
trace(affScore(12345678, 0)); // 12,345,678

      

+1


source


Use the NumberFormatter class:

import flash.globalization.NumberFormatter;

var nf:NumberFormatter = new NumberFormatter("en_US");
var numberString:String = nf.formatNumber(1234567.89);
trace("Formatted Number:" + numberString);

// Formatted Number:1,234,567.89

      

+1


source







All Articles