“Be inspired, enjoy your work, keep learning and never forget to play.” — gskinner

Insert Thousands Delimiter using RegExps in Actionscript

Posted: May 28th, 2008 | Author: Tiago Bilou | Filed under: Actionscript3, Code | Tags: | 7 Comments »

Today I spend a considerable amount of time trying to change this (10000000) into this (10.000.000). Because I don't think anyone should have to go throught the same ordeal here's a little function that will do it for you.

References:


7 Comments on “Insert Thousands Delimiter using RegExps in Actionscript”

  1. 1 Dave said at 1:56 am on May 28th, 2008:

    Nice. Thanks for posting.

  2. 2 Sev said at 11:38 pm on June 9th, 2008:

    While I really like regexp (especially for find/replace in code), I think it’s too slow for simple text operations. I actually try to avoid it wherever possible due to some tests I’ve done. To give an example, I recoded the functionality of the above function with some simple (and not at all polished) text ops:

    public function thousands_fast(value:String):String
    {
    var l : int = value.length;
    var result : String = value.substr( Math.max(l-4,0) , 3 );
    for ( var i : int = l-4 ; i >= 0 ; i– ) {
    if ( ( l – i – 1 ) % 3 == 0 ) result = ‘.’ + result;
    result = value.substr(i,1) + result;
    }
    return result;
    }

    It doesn’t look nice. But it’s MORE THAN 10x faster! To me, that makes all the difference. I even do complex parsing all by myself (you know, indexOf, split, substr and that kind of stuff), just because in the end it’s so much faster. Unfortunately. (I know of course that you made this for fun and not because you thought it would be the best solution, but hey, I kind of felt the need to shout this out for once ;-)

  3. 3 Sev said at 11:54 pm on June 9th, 2008:

    this one is even faster (the longer the string, the better the improvement)

    ‘100000000000000000000000000000000000000000000000 00000000000000000000′ – 60x faster!!!

    ‘1000000′ – 10x faster

    i’m sure other people could improve even more (i love speed!) :)

    public function thousands_char(value:String):String
    {
    var l : int = value.length;
    var result : String = ”;
    var i : int = 0;
    while ( i 0 && (l-i) % 3 == 0 ) result += ‘.’;
    result += value.charAt(i);
    i++;
    }
    return result;
    }

  4. 4 Idoru said at 7:44 am on June 10th, 2008:

    Nice one Sev,
    And speed is important but sometimes having a regExp just gives you a little more flexibility, but clearly you’ve got it right on the speed part :)

  5. 5 Hak said at 5:24 pm on June 27th, 2008:

    Is this code to speed up the movement and take less strain from the CPU? I need to decrease the usage of the cpu when using papervision. How do I do that. I know this is for Away3D but we are using papervision. What is the upside of using Away3D as compared to papervision?

  6. 6 Idoru said at 6:46 pm on July 2nd, 2008:

    Hi Hak,
    This is just to deal with strings, it has nothing to do with either away3d or pv3d. But if you’re after speed PV3d is the way to go.

  7. 7 Flash Animation said at 1:27 am on July 8th, 2008:

    nice thx man :)


Leave a Reply