PDA

View Full Version : String formatting


mikehump
11-10-2002, 06:01 AM
I'm doing a little Javascripting and need to be able to convert
a string like:

100000000000

to

100,000,000,000

Ultimately I will be using it in an alert(); function.

e.g alert("You scored a total of " + total + " points! Good Job!")

Do I need to go in a direction that would involve regular expressions?

I tried this but it doesn't work:


TestString = TestString.replace(“XXX,XXX,XXX,XXX”, TestString);

alert("TestString.replace(“XXX,XXX,XXX,XXX”, TestString) = " + TestString);

Thanks for any help you can provide.

Mike Humphrey
Catholic Apologist and Web site Developer
http://www.mikeswebsites.com
http://www.cpats.org


:confused:

joh6nn
11-10-2002, 06:13 AM
function FormatNumber(n){
var t = "";
var i, j = 0;
for (i = n.length - 1; i >= 0; i--) {
t = n.charAt(i) + t;
if (i && ((++j % 3) == 0)) { t = L_Delimiter_Text + t; }
}
return t;
}

var num = "100000000000"
FormatNumber(num);

mikehump
11-10-2002, 07:01 AM
Hi John

I almost have it.

This work OK:

var num = 10000000000;
var strnum = '10000000000';


alert("FormatNumber(strnum) = " + FormatNumber(strnum));

but this doesn't;

strnum = toString(num);
alert("FormatNumber(strnum) = " + FormatNumber(strnum));

Any ideas.

Thanks for your help.

Mike

mikehump
11-10-2002, 07:18 AM
Hi John,


Thanks for the script, I never would have figured it out.

I figured out the other problem I had with the second post to you.

No Need to reply.

Mike

Solution:

alert("FormatNumber(num.toString()) = " + FormatNumber(num.toString()));


Mike