-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #780 from Ekep-Obasi/feature/add-format-stats
feat: implement number formatting with commas
- Loading branch information
Showing
4 changed files
with
63 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { formatNumberWithCommas } from '../index' | ||
|
||
describe('formatNumberWithCommas', () => { | ||
it('should format a number with default locale (en-US)', () => { | ||
const result = formatNumberWithCommas(1000000) | ||
|
||
expect(result).toBe('1,000,000') | ||
}) | ||
|
||
it('should handle a negative number', () => { | ||
const result = formatNumberWithCommas(-1234567.89) | ||
|
||
expect(result).toBe('-1,234,567.89') | ||
}) | ||
|
||
it('should handle a decimal number', () => { | ||
const result = formatNumberWithCommas(1234.56) | ||
|
||
expect(result).toBe('1,234.56') | ||
}) | ||
|
||
it('should handle zero', () => { | ||
const result = formatNumberWithCommas(0) | ||
|
||
expect(result).toBe('0') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Formats a number with commas as thousands separators using the Intl.NumberFormat constructor. | ||
* | ||
* @param {number} number - The number to be formatted. | ||
* @param {string} [locale='en-US'] - The locale to use for formatting. Defaults to 'en-US'. | ||
* @returns {string} A string representation of the formatted number. | ||
*/ | ||
|
||
export const formatNumberWithCommas = (number: number, locale = 'en-US') => { | ||
try { | ||
const formatter = new Intl.NumberFormat(locale) | ||
|
||
return formatter.format(number) | ||
} catch (error) { | ||
console.error('Error formatting number:', error) | ||
|
||
return number.toString() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters