Added ES6 local support for formatNumber helper as per #2951 (#3099)

This commit is contained in:
Braunson Yager 2021-10-13 14:48:37 -04:00 committed by GitHub
parent 894707c61a
commit 7fa4189fc1

View File

@ -1,11 +1,13 @@
import app from '../../forum/app';
/** /**
* The `formatNumber` utility localizes a number into a string with the * The `formatNumber` utility localizes a number into a string with the
* appropriate punctuation. * appropriate punctuation based on the provided locale otherwise will default to the users locale.
* *
* @example * @example
* formatNumber(1234); * formatNumber(1234);
* // 1,234 * // 1,234
*/ */
export default function formatNumber(number: number): string { export default function formatNumber(number: number, locale: string = app.data.locale): string {
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); return new Intl.NumberFormat(locale).format(number);
} }