import currency from 'currency.js'; export const formatShort = (n?: number): string => { if (!n) { return '0'; } if (n < 1e3) { return n.toString(); } if (n >= 1e3 && n < 1e6) { return +(n / 1e3).toFixed(3) + 'K'; } if (n >= 1e6 && n < 1e9) { const currencyFormatted = (+(n / 1e3).toFixed(3)).toString(); const thousandFormatted = /\B(?=(\d{3})+(?!\d))/g; return currencyFormatted.replace('.', ',').replace(thousandFormatted, '.') + 'K'; } if (n >= 1e9 && n < 1e12) { return +(n / 1e9).toFixed(1) + 'B'; } if (n >= 1e12) { return +(n / 1e12).toFixed(1) + 'T'; } }; export const formatShortV2 = (n?: number, type?: number): string => { if (!n) { return '0'; } if (type === 1) { const num = n.toFixed(); const result = currency(num, { symbol: '', precision: 0, decimal: ',', separator: '.' }).format(); return result.toString(); } if (type === 2) { const result = currency(parseFloat(n.toFixed(2)), { symbol: '', precision: 0, decimal: ',', separator: '.' }).format(); return result.toString(); } if (n >= 1e3 && n < 1e6) { return +(n / 1e3).toFixed(3) + ' ribu'; } if (n >= 1e6 && n < 1e9) { return +(n / 1e6).toFixed(3) + ' juta'; } if (n >= 1e9 && n < 1e12) { return +(n / 1e9).toFixed(2) + 'B'; } if (n >= 1e12) { return +(n / 1e12).toFixed(2) + 'T'; } };