flash actionscript:
Hi, Anyone that happends to know if there is a good way of preformat a string for currency? Example; I have a number = "1234567" that i want to look like this "1.234.567" or similar and eventually include a currency symbol such as "$". I have checked the manual but can not find it. Cheers
check canfieldstudios.com:
There's no built in way. Here's a function I made awhile back: //v2 strSeperator = function(num,delimiter){//takes long number(or string) and incrementally inserts a character num=num.toString();//convert Numbers to String pos=num.length;//var 'pos' is used to add a seperator at while(pos>3){//while loop, starts at String right moves to left pos-=3;//move left to next seperator point n2=num.slice(pos,num.length);//get all characters after seperator point as new substring n1=num.slice(0,pos);//get all characters before seperator point as new substring num=n1+delimiter+n2;//combine before-substring + dilimiter + after-substring as new 'num' String } return num;//return 'num' to where function is called } //example myNum = 7625428765237; myNumFormatted = strSeperator(myNum,".") trace(myNumFormatted);
[q][i]Originally posted by: [b][b]NSurveyor[/b][/b][/i] abeall, your code doesn't like numbers with decimals! It's a simple fix, just pull out the decimal part, do the stuff, then reattach the decimal part.[/q] abeal or NSurveyor, could you please edit the code so it can handle decimals? Cheers
Good call, NS! Here's my fix: replace this: pos=num.length; with this: pos = (num.indexOf('.')==-1) ? num.length : num.indexOf('.'); What that ugly line does is check to see if there's a "." - that's what the ? and : are for - in the number, if there is a '.' then it starts at the dot, otherwise it starts at the end of the number(actually at this point converted to string). Seems to work, the one time I tested it :-)
One slight fix, for numbers like: -123345123, which gets formatted as: -,123,345,123 (because it is negative and the number of digits is a multiple of 3) sign = num<0 ? '-' : ''; num=Math.abs(num).toString(); ... return sign+num;
Don't see what you're looking for? Try a search.
|