all groups > flash actionscript > august 2006 >
You're in the

flash actionscript

group:

Format a text field


Format a text field Pe_ka
8/20/2006 8:19:13 PM
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
Re: Format a text field kglad
8/20/2006 9:41:40 PM
check canfieldstudios.com:

Re: Format a text field abeall
8/20/2006 11:14:29 PM
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);
Re: Format a text field NSurveyor
8/21/2006 3:59:16 AM
Re: Format a text field Pe_ka
8/22/2006 9:42:00 PM
[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


Re: Format a text field abeall
8/22/2006 11:59:29 PM
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 :-)
Re: Format a text field NSurveyor
8/23/2006 12:59:29 AM
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;


AddThis Social Bookmark Button