File size: 691 Bytes
8ecab61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
 * Format large numbers with K/M/B suffixes.
 *
 * @param {string|number} value - The number to format
 * @returns {string} Formatted number with suffix
 *
 * Examples:
 *   formatNumber(1500) => "1.5K"
 *   formatNumber(36309000) => "36.3M"
 *   formatNumber(1200000000) => "1.2B"
 */
export function formatNumber(value) {
  const num = typeof value === 'string' ? parseFloat(value) : value

  if (isNaN(num)) {
    return '0'
  }

  if (num >= 1_000_000_000) {
    return (num / 1_000_000_000).toFixed(1) + 'B'
  }
  if (num >= 1_000_000) {
    return (num / 1_000_000).toFixed(1) + 'M'
  }
  if (num >= 1_000) {
    return (num / 1_000).toFixed(1) + 'K'
  }
  return num.toString()
}