2020-10-08 12:34:48 -04:00
|
|
|
// @flow
|
2022-05-06 10:31:36 -03:00
|
|
|
import { getFormatter } from './intlNumberFormat';
|
2020-10-08 12:34:48 -04:00
|
|
|
|
2022-04-26 12:28:23 -03:00
|
|
|
export function formatNumber(num: number, numberOfDigits?: number, short: boolean = false): string {
|
2022-04-28 09:05:05 -03:00
|
|
|
const language = localStorage.getItem('language') || undefined;
|
2022-04-26 12:28:23 -03:00
|
|
|
const safePrecision = Math.min(20, numberOfDigits || 0);
|
2022-05-06 10:31:36 -03:00
|
|
|
const formatter = getFormatter(language, {
|
2022-04-26 12:28:23 -03:00
|
|
|
maximumFractionDigits: safePrecision,
|
|
|
|
notation: short ? 'compact' : 'standard',
|
|
|
|
compactDisplay: 'short',
|
|
|
|
});
|
|
|
|
return formatter.format(num);
|
|
|
|
}
|
|
|
|
|
2021-01-12 10:25:51 -05:00
|
|
|
export function formatNumberWithCommas(num: number, numberOfDigits?: number): string {
|
|
|
|
return num.toLocaleString('en', { minimumFractionDigits: numberOfDigits !== undefined ? numberOfDigits : 8 });
|
2020-10-08 12:34:48 -04:00
|
|
|
}
|
2022-02-25 15:01:14 -05:00
|
|
|
|
|
|
|
export function isTrulyANumber(num: number) {
|
|
|
|
// typeof NaN = 'number' but NaN !== NaN
|
|
|
|
return typeof num === 'number' && num === num; // eslint-disable-line
|
|
|
|
}
|