Page 1 of 1

Commify!

Posted: Tue Oct 09, 2007 9:59 pm
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

Posted: Tue Oct 09, 2007 11:19 pm
by Luke

Posted: Wed Oct 10, 2007 2:41 am
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.

Posted: Wed Oct 10, 2007 3:38 am
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;
}

Posted: Wed Oct 10, 2007 10:32 am
by Luke
sorry I didn't notice this was in client side.

Posted: Thu Oct 11, 2007 2:32 am
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

Posted: Thu Oct 11, 2007 2:38 pm
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;
}

Posted: Thu Oct 11, 2007 4:51 pm
by Luke
javascript is sexy

Posted: Thu Oct 11, 2007 5:31 pm
by Benjamin
The Ninja Space Goat wrote:javascript is sexy
You must be wearing beer goggles.

Posted: Thu Oct 11, 2007 9:59 pm
by Kieran Huggins
I second that -- javascript is HAWT!

"Lisp in C's clothing"

Posted: Sat Oct 13, 2007 5:52 pm
by JellyFish
even more sweet on the iPhone. :D