| 34 | } |
| 35 | |
| 36 | export function formatNumber(val: number): string { |
| 37 | if (val >= 1e12) { |
| 38 | return `${toFixed(val / 1e12)}T`; |
| 39 | } |
| 40 | if (val >= 1e9) { |
| 41 | return `${toFixed(val / 1e9)}B`; |
| 42 | } |
| 43 | if (val >= 1e6) { |
| 44 | return `${toFixed(val / 1e6)}M`; |
| 45 | } |
| 46 | if (val >= 1e3) { |
| 47 | return `${toFixed(val / 1e3)}k`; |
| 48 | } |
| 49 | if (val >= 1) { |
| 50 | return val.toString(); |
| 51 | } |
| 52 | if (val >= 1e-3) { |
| 53 | return `${toFixed(val * 1e3)}m`; |
| 54 | } |
| 55 | if (val >= 1e-6) { |
| 56 | return `${toFixed(val * 1e6)}µ`; |
| 57 | } |
| 58 | if (val >= 1e-9) { |
| 59 | return `${toFixed(val * 1e9)}n`; |
| 60 | } |
| 61 | if (val >= 1e-12) { |
| 62 | return `${toFixed(val * 1e12)}p`; |
| 63 | } |
| 64 | return val.toString(); |
| 65 | } |
| 66 | |
| 67 | function toFixed(val: number) { |
| 68 | return val.toPrecision(3); |