lbry-desktop/ui/util/number.js

22 lines
814 B
JavaScript
Raw Normal View History

// @flow
2022-04-26 17:28:23 +02:00
export function formatNumber(num: number, numberOfDigits?: number, short: boolean = false): string {
const language = localStorage.getItem('language') || undefined;
2022-04-26 17:28:23 +02:00
const safePrecision = Math.min(20, numberOfDigits || 0);
const formatter = new Intl.NumberFormat(language, {
2022-04-26 17:28:23 +02:00
maximumFractionDigits: safePrecision,
notation: short ? 'compact' : 'standard',
compactDisplay: 'short',
});
return formatter.format(num);
}
export function formatNumberWithCommas(num: number, numberOfDigits?: number): string {
return num.toLocaleString('en', { minimumFractionDigits: numberOfDigits !== undefined ? numberOfDigits : 8 });
}
2022-02-25 21:01:14 +01:00
export function isTrulyANumber(num: number) {
// typeof NaN = 'number' but NaN !== NaN
return typeof num === 'number' && num === num; // eslint-disable-line
}