Commify!

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Commify!

Post by JellyFish »

Hey, how would I add commas to a string of numbers? For example, 24567 should be 24,567 and 212356 should be 212,356. But here's the trick part. I only would like to add commas to the string where there is no comma. That is, 1000,000 should be 1,000,000.

I'll look more into it later, but my head is scrabble from all the Regular Expressions...

Thanks in advance! :D
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

The Ninja Space Goat wrote:http://us.php.net/number_format
Err. Sorry, it has to be with javascript. I'm sorry I didn't specify that earlier.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

from http://www.mredkj.com/javascript/numberFormat.html

Code: Select all

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

sorry I didn't notice this was in client side.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

The Ninja Space Goat wrote:sorry I didn't notice this was in client side.
YOU BETTER BE SORRY!!!



















No, Just kidding. You don't need to apologize. In fact, your mistake is my benifit. I realized that I do indeed need a server-side version of this. :lol: Thanks man.


@Kieran Huggins

This is awesome, it's exactly what I'm looking for! Thank you so much.

But how does it work exactly? Where could I find a tutorial explaining it step be step? If it's to much to ask then don't bother.

Thanks again. :D
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Why not? I'm supposed to teach my first javascript class this evening, as luck would have it :-)

Code: Select all

function addCommas(nStr)
{
        // create an empty string
        nStr += '';

        // create an array with 2 parts: the part to the left of the decimal, and the part to the right
        // anything to the right of the decimal should be saved for later, as it's not comma'd
        x = nStr.split('.');
        x1 = x[0]; // the left portion (the part we're changing)
        x2 = x.length > 1 ? '.' + x[1] : ''; // the right portion (the part we're leaving alone)

        // here we're building a regex string to match 2 groups of numbers, 3 on the right, 
        // and an unlimited number before that
        var rgx = /(\d+)(\d{3})/;
        
        // as long as that pattern matches the string we're changing....
        while (rgx.test(x1)) {

                // add a comma between groups 1 and 2
                x1 = x1.replace(rgx, '$1' + ',' + '$2');
        }

        // finally, add the decimal part of the string back on and return the entire string.
        return x1 + x2;
}
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

javascript is sexy
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

The Ninja Space Goat wrote:javascript is sexy
You must be wearing beer goggles.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I second that -- javascript is HAWT!

"Lisp in C's clothing"
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

even more sweet on the iPhone. :D
Post Reply