DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Comma Numbers

01.29.2006
| 689 views |
  • submit to reddit
        

function comma(number) { // oh dear god...
	str = new String(number);
	var val = new String();
	var num = str.length % 3;
	if (num == 0) { num = 3; }
	while (str.length > 0) {
		val += str.substring (0, num) + ",";
		str = str.substring (num);
		num = 3;
	}
	return val.substring (0, val.length - 1);
}