(value: number)
| 1 | import type { PaddleProductLineItem } from '../graphql/paddle'; |
| 2 | |
| 3 | export function largeNumberFormat(value: number): string | null { |
| 4 | if (typeof value !== 'number') { |
| 5 | return null; |
| 6 | } |
| 7 | let newValue = value; |
| 8 | const suffixes = ['', 'K', 'M', 'B', 'T']; |
| 9 | let suffixNum = 0; |
| 10 | while (newValue >= 1000) { |
| 11 | newValue /= 1000; |
| 12 | suffixNum += 1; |
| 13 | } |
| 14 | if (suffixNum > 0) { |
| 15 | const remainder = newValue % 1; |
| 16 | // If the remainder is very small (< 0.05), show no decimal places |
| 17 | // If the remainder is significant, show one decimal place |
| 18 | const decimalPlaces = remainder >= 0 && remainder < 0.05 ? 0 : 1; |
| 19 | const roundedValue = |
| 20 | Math.round(newValue * 10 ** decimalPlaces) / 10 ** decimalPlaces; |
| 21 | // Check if the rounded value is a whole number |
| 22 | const isWholeNumber = roundedValue % 1 === 0; |
| 23 | const finalDecimalPlaces = isWholeNumber ? 0 : decimalPlaces; |
| 24 | return roundedValue.toFixed(finalDecimalPlaces) + suffixes[suffixNum]; |
| 25 | } |
| 26 | return newValue.toString(); |
| 27 | } |
| 28 | |
| 29 | export const getRandom4Digits = (leftPad?: string): string => { |
| 30 | const random = Math.floor(1000 + Math.random() * 9000); |
no outgoing calls
no test coverage detected