Skip to content

Commit 684d114

Browse files
feat(num): add formatNumber function with caching of the formatter
1 parent 930dc62 commit 684d114

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/num/formatNumber.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let numberFormat: Intl.NumberFormat | undefined = undefined
2+
let numberFormatLocale: string | undefined = undefined
3+
4+
export function formatNumber(num: number, locale?: string) {
5+
if (locale === numberFormatLocale && numberFormat) {
6+
return numberFormat.format(num)
7+
}
8+
numberFormat = new Intl.NumberFormat(locale)
9+
numberFormatLocale = locale
10+
return numberFormat.format(num)
11+
}

0 commit comments

Comments
 (0)