Skip to main content

Format a number with commas (commify) as the thousands separator.

/**
 * Print a number with commas as the thousands separator.
 *
 * @example 1000 => "1,000"
 * @param {number} x The numnber to print
 * @return {string} The number with commas as the thousands separator
 *
 * @link https://stackoverflow.com/a/2901298
 */
function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}