(num: number)
| 26 | * and the order of magnitude (e.g., thousand, million, billion). |
| 27 | */ |
| 28 | export function getNumberWithMagnitude(num: number): { |
| 29 | rounded: number; |
| 30 | roundedTo2: number; |
| 31 | orderOfMagnitude: string; |
| 32 | } { |
| 33 | const units = [ |
| 34 | "", |
| 35 | "thousand", |
| 36 | "million", |
| 37 | "billion", |
| 38 | "trillion", |
| 39 | "quadrillion", |
| 40 | "quintillion", |
| 41 | "sextillion", |
| 42 | "septillion", |
| 43 | "octillion", |
| 44 | "nonillion", |
| 45 | "decillion", |
| 46 | ]; |
| 47 | let unitIndex = 0; |
| 48 | let roundedNum = num; |
| 49 | |
| 50 | while (roundedNum >= 1000) { |
| 51 | roundedNum /= 1000; |
| 52 | unitIndex++; |
| 53 | } |
| 54 | |
| 55 | const unit = units[unitIndex] ?? "unknown"; |
| 56 | |
| 57 | return { |
| 58 | rounded: Math.round(roundedNum), |
| 59 | roundedTo2: roundTo2(roundedNum), |
| 60 | orderOfMagnitude: unit, |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Abbreviates a large number with a suffix (k, m, b, etc.) representing its order of magnitude. |
no test coverage detected