// Converts "1234567.89" => "1,234,567.89" function formatNumberWithCommas(number) { const parts = number.split('.') return ('' + parts[0]) // Convert to a string .split('').reverse().join('') // Reverse the order of the characters (which are all digits at this point) .replace(/(...)/g, '$1,') // Insert a comma after every three digits .split('').reverse().join('') // Un-reverse the characters .replace(/^,/, '') // Remove any commas that were added to the beginning (i.e. if the number of digits was a multiple of three) .concat(parts[1] ? '.' + parts[1] : '') // Re-add the decimal, if any }