Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

number Format function in Flash MX? 1

Status
Not open for further replies.

ColdFusionKing

Programmer
Dec 5, 2002
145
GB
Is there a function in flash which is similar to the numberFormat(number,[mask]) ColdFusion function? I want to show the result in default format, adding the comma
for the thousands place. Please can somebody help? Many Thanks
 
for ex I want the number 40000 to be displayed like this: 40,000. I'm a flash newbie. There has be a function which does this for you.
 
not that I am aware of (but that totally does not mean anything) what I would probably do is build a function to perform what you need... string it, measure the length and insert a comma if need be....
 
this will work

Code:
Number.prototype.commaFormat = function() {
	n = this.toString(); insPTR = 1;
	if (n.indexOf(".") == -1 ){
		t = "";	n = n.split("");
	}else{
		t = n.substr(n.indexOf("."), n.length);
		n = n.substr(0, n.indexOf(".")).split("");
	}
	for (ptr in n) {
		t = n[ptr]+t;
		if (insPTR++ == 3) {
			insPTR = 1;
			t = (isNaN(n[0]) && ptr > 1) || (!isNaN(n[0]) && ptr >= 1) ? ","+t : t;
		}
	}
	return t;
};

mynum = 123456789;
trace(mynum = mynum.commaFormat());
 
Actually, I can't get it to work. This is what I'm trying to do.

Number.prototype.commaFormat = function() {
n = this.toString(); insPTR = 1;
if (n.indexOf(".") == -1 ){
t = ""; n = n.split("");
}else{
t = n.substr(n.indexOf("."), n.length);
n = n.substr(0, n.indexOf(".")).split("");
}
for (ptr in n) {
t = n[ptr]+t;
if (insPTR++ == 3) {
insPTR = 1;
t = (isNaN(n[0]) && ptr > 1) || (!isNaN(n[0]) && ptr >= 1) ? ","+t : t;
}
}
return t;
};


mynum = "123456789,34234,23423423,24523423,25234523";
var list_temp = mynum.split(",");
for (i=0; i < list_temp.length; i++){
tmpvar = list_temp;
trace(tmpvar.commaFormat());
}

The trace returns &quot;undefined&quot;. Is there something wrong with my code?
 
Code:
try tempvar = list_temp[i]
or tempvar = Number(list_temp[i])
 
oh that was a typing mistake (tempvar = list_temp and not tempvar = list_temp). But I've tried it and it still comes back with the &quot;undefined&quot; trace message. Does it work for you?
 
tested your code works with

Code:
tempvar = Number(list_temp[i])
trace(tempvar)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top