A thousands separator groups the digits of a number into runs of three — turning 1234567 into 1,234,567 — so a person can read the magnitude at a glance instead of counting zeros. You will write thousandsSeparator(value, options), which returns that grouped string. It has to group the integer part from the right, leave the decimals alone, keep a negative sign in front, and honor a custom separator and decimal mark. Browsers ship this in Intl.NumberFormat, but here you build the grouping by hand.
thousandsSeparator(
value: number | string,
options?: { separator?: string; decimal?: string }
): string
// separator defaults to ",", decimal defaults to "."
thousandsSeparator(1234567.89); // '1,234,567.89'
thousandsSeparator(-1000); // '-1,000'
thousandsSeparator(999); // '999'
thousandsSeparator(1000000); // '1,000,000'
thousandsSeparator(1234567, { separator: ' ' }); // '1 234 567'
thousandsSeparator(1234567.89, { separator: '.', decimal: ',' }); // '1.234.567,89'
thousandsSeparator('1234567.89'); // '1,234,567.89'
- before the first digit.value may be a JS number or a numeric string. A string lets a caller preserve exact decimals, such as a trailing zero that a number would drop.Intl.NumberFormat or Number.prototype.toLocaleString.1e21), and you do not need to validate non-numeric junk.