Insert Thousands Delimiter using RegExps in Actionscript
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:
May 28th, 2008 at 1:56 am
Nice. Thanks for posting.
June 9th, 2008 at 11:38 pm
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
June 9th, 2008 at 11:54 pm
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;
}
June 10th, 2008 at 7:44 am
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
June 27th, 2008 at 5:24 pm
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?
July 2nd, 2008 at 6:46 pm
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.
July 8th, 2008 at 1:27 am
nice thx man