This seemed to take a disproportionate amount of digging to find. So for future reference here is a neat Javascript number formatting function that adds commas to large numbers. Was found at http://www.mredkj.com/javascript/nfbasic.html

		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;
		}

2 Thoughts to “Javascript Number Format Function for Adding Commas”

  1. Number(10000).toLocaleString(‘en’) == “10,000” //will do the same

  2. > Number(10000).toLocaleString(‘en’) == “10,000” //will do the same

    toLocaleString(‘en’) will only work in a few browsers such as Chrome (as of October 2013.) For example, it doesn’t work with the most recent iPhone or Safari browsers.

Leave a Comment