// @todo: join with "utils/common/formatters" /** * Converts a number to short money format. * @param amount number what will be formatted */ const moneyFormaterShort = (amount: number | undefined | null | string) => { if (!amount) { if (amount === 0) return amount; return 'NA'; } if (typeof amount === 'string') { if (!isNaN(+amount)) { amount = +amount; } else { return amount; } } if (amount < 0) amount *= -1; if (amount < 100) return `$${amount}`; if (amount >= 100 && amount < 1000000) return `$${Math.round((amount / 1000 + Number.EPSILON) * 100) / 100}k`; if (amount >= 1000000 && amount < 1000000000) return `$${Math.round((amount / 1000000 + Number.EPSILON) * 100) / 100}m`; // if (amount >= 1000000000) return `$${Math.round((amount / 1000000000 + Number.EPSILON) * 100) / 100}b`; }; /** * Converts a number to full money format. * @param amount number what will be formatted * @param fraction number of digits after the dot (default = 0) */ const moneyFormaterFull = (amount: number | string, fraction = 0) => { if (typeof amount === 'string') { if (!isNaN(+amount)) { amount = +amount; } else { return amount; } } return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: fraction }).format(amount); }; /** * Converts a number to view number format. * @param number number what will be formatted */ const numberFormat = (number: number) => new Intl.NumberFormat('en-US').format(number); export { moneyFormaterShort, moneyFormaterFull, numberFormat };