common: add formatNumber - use toLocaleString and support current application locale + custom options

This commit is contained in:
David Sevilla Martin 2020-03-07 10:00:33 -05:00
parent 3bf7f6f85b
commit 8ea7f9bc17
No known key found for this signature in database
GPG Key ID: F764F1417E16B15F

View 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);
}