mirror of
https://github.com/flarum/framework.git
synced 2025-01-22 12:54:59 +08:00
common: add formatNumber - use toLocaleString and support current application locale + custom options
This commit is contained in:
parent
3bf7f6f85b
commit
8ea7f9bc17
16
js/src/common/utils/formatNumber.ts
Normal file
16
js/src/common/utils/formatNumber.ts
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
/**
|
||||||
|
* The `formatNumber` utility localizes a number into a string with the
|
||||||
|
* appropriate punctuation.
|
||||||
|
*
|
||||||
|
* @param number Number to format
|
||||||
|
* @param options Maximum significant digits or formatting options object
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* formatNumber(1234);
|
||||||
|
* // 1,234
|
||||||
|
*/
|
||||||
|
export default function formatNumber(number: number, options: number | Intl.NumberFormatOptions = {}): string {
|
||||||
|
const config = typeof options === 'number' ? { maximumSignificantDigits: options } : options;
|
||||||
|
|
||||||
|
return number.toLocaleString(app.translator.locale, config);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user