commaFormat
The commaFormat
function formats a given number or string by inserting commas at regular intervals for better readability. By default, commas are inserted every three digits.
#
Function Signatureconst commaFormat = (inputNumber: number | string, count: number = 3) => string;
#
ParametersinputNumber: number | string
: The number or string to be formatted.
count: number
(optional): The number of digits between each comma. Defaults to 3.
#
Returnsstring
: A string representation of the number with commas inserted at the specified intervals.
#
Example Usage#
Example 1: Default Formatting (Every 3 Digits)console.log(commaFormat(1000)); // Output: '1,000'console.log(commaFormat(1000000)); // Output: '1,000,000'console.log(commaFormat(1234567890)); // Output: '1,234,567,890'
#
Example 2: Formatting String Representations of Numbersconsole.log(commaFormat('1000')); // Output: '1,000'console.log(commaFormat('1000000')); // Output: '1,000,000'console.log(commaFormat('1234567890')); // Output: '1,234,567,890'
#
Example 3: Handling Decimal Numbersconsole.log(commaFormat(1234.56)); // Output: '1,234.56'console.log(commaFormat('1234.56')); // Output: '1,234.56'
#
Example 4: Handling Numbers with Leading Zerosconsole.log(commaFormat('0001234')); // Output: '0,001,234'
#
Example 5: Handling Falsy Inputsconsole.log(commaFormat(0)); // Output: '0'console.log(commaFormat('')); // Output: '0'
#
Example 6: Formatting Negative Numbersconsole.log(commaFormat(-1000)); // Output: '-1,000'console.log(commaFormat('-1000000')); // Output: '-1,000,000'
#
Example 7: Custom Formatting (Every 2 Digits)console.log(commaFormat(123456, 2)); // Output: '12,34,56'console.log(commaFormat('12345678', 2)); // Output: '12,34,56,78'
#
Example 8: Custom Formatting (Every 4 Digits)console.log(commaFormat(1234567890, 4)); // Output: '12,3456,7890'console.log(commaFormat('12345678901234', 4)); // Output: '12,3456,7890,1234'
#
ExplanationRegex for Comma Insertion
: The function uses a regular expression to insert commas. TheRegExp
is dynamically created based on thecount
parameter, which determines how many digits should appear between commas.Input Type Handling
: The function first converts theinputNumber
to a string, ensuring that both numeric and string inputs are treated consistently.Falsy Input
: If the inputNumber is falsy (like0
or an empty string), the function returns '0
'.Negative Numbers
: The function handles negative numbers correctly by placing the comma after the negative sign.