FormatNumber formats large numbers in a human-readable way (e.g., "1k", "1.2k", "1.12M")
(n int)
| 672 | |
| 673 | // FormatNumber formats large numbers in a human-readable way (e.g., "1k", "1.2k", "1.12M") |
| 674 | func FormatNumber(n int) string { |
| 675 | if n == 0 { |
| 676 | return "0" |
| 677 | } |
| 678 | |
| 679 | f := float64(n) |
| 680 | |
| 681 | if f < 1000 { |
| 682 | return strconv.Itoa(n) |
| 683 | } else if f < 1000000 { |
| 684 | // Format as thousands (k) |
| 685 | k := f / 1000 |
| 686 | if k >= 100 { |
| 687 | return fmt.Sprintf("%.0fk", k) |
| 688 | } else if k >= 10 { |
| 689 | return fmt.Sprintf("%.1fk", k) |
| 690 | } else { |
| 691 | return fmt.Sprintf("%.2fk", k) |
| 692 | } |
| 693 | } else if f < 1000000000 { |
| 694 | // Format as millions (M) |
| 695 | m := f / 1000000 |
| 696 | if m >= 100 { |
| 697 | return fmt.Sprintf("%.0fM", m) |
| 698 | } else if m >= 10 { |
| 699 | return fmt.Sprintf("%.1fM", m) |
| 700 | } else { |
| 701 | return fmt.Sprintf("%.2fM", m) |
| 702 | } |
| 703 | } else { |
| 704 | // Format as billions (B) |
| 705 | b := f / 1000000000 |
| 706 | if b >= 100 { |
| 707 | return fmt.Sprintf("%.0fB", b) |
| 708 | } else if b >= 10 { |
| 709 | return fmt.Sprintf("%.1fB", b) |
| 710 | } else { |
| 711 | return fmt.Sprintf("%.2fB", b) |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | // FormatTokens formats a token count as a compact human-readable string. |
| 717 | // Zero is rendered as "-"; values below 1000 are rendered as plain integers; |
no outgoing calls