(num: number)
| 12 | Example: 1000000000000000000000000000000 -> $1000000000000000000 |
| 13 | */ |
| 14 | export default function formatNumber(num: number): string { |
| 15 | if (num < 0) return "Unknown"; // If the number is negative, return "unknown |
| 16 | |
| 17 | // Always round the number to two decimal places |
| 18 | num = Math.round(num * 100) / 100; |
| 19 | |
| 20 | if (num >= 1e3 && num < 1e6) return "$" + (num / 1e3).toFixed(1) + "k"; // thousands |
| 21 | if (num >= 1e6 && num < 1e9) return "$" + (num / 1e6).toFixed(1) + " Million"; // millions |
| 22 | if (num >= 1e9 && num < 1e12) |
| 23 | return "$" + (num / 1e9).toFixed(1) + " Billion"; // billions |
| 24 | if (num >= 1e12) return "$" + (num / 1e12).toFixed(1) + " Trillion"; // trillions |
| 25 | |
| 26 | return "$" + num.toString(); |
| 27 | } |
no outgoing calls
no test coverage detected