user3406023
user3406023

Reputation: 39

AS3-Flash cs6 How to make numbers have a comma?

I am making a game that when you click on the Monster your score gets +1. But when your score goes over 1000 I would like it like this 1,000 rather than 1000. I am not sure how to do this as I have not learnt much action script. I have embed number and punctuation into the font. Here is my code so far:

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 will greatly appreciated.

Upvotes: 3

Views: 377

Answers (4)

T Graham
T Graham

Reputation: 1339

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

Upvotes: 1

akmozo
akmozo

Reputation: 9839

To show the score with comma, you can do like this : ( 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 like this :

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 that can help you.

Upvotes: 0

Martyn Shutt
Martyn Shutt

Reputation: 1706

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

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 tested it on some pretty large numbers, and it seems to work just fine. There's no upper limit on the size of the number, so;

trace (formatNum("10000000000000000000"));

Will output:

10,000,000,000,000,000,000

So in your example, you could use it thusly;

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

(This is casting the type implicitly rather than explicitly using toString();, but either method is fine. Casting just looks a little neater in function calls)

Upvotes: 1

helloflash
helloflash

Reputation: 2457

You can do like that:

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

Upvotes: 1

Related Questions