humanInt formats n with thousands separators (e.g. 1041234 → "1,041,234"). Pure Go, no third-party deps.
(n int64)
| 160 | // humanInt formats n with thousands separators (e.g. 1041234 → "1,041,234"). |
| 161 | // Pure Go, no third-party deps. |
| 162 | func humanInt(n int64) string { |
| 163 | if n < 0 { |
| 164 | return "-" + humanInt(-n) |
| 165 | } |
| 166 | s := fmt.Sprintf("%d", n) |
| 167 | // Insert commas every 3 digits from the right. |
| 168 | if len(s) <= 3 { |
| 169 | return s |
| 170 | } |
| 171 | var b strings.Builder |
| 172 | rem := len(s) % 3 |
| 173 | if rem > 0 { |
| 174 | b.WriteString(s[:rem]) |
| 175 | } |
| 176 | for i := rem; i < len(s); i += 3 { |
| 177 | if i > 0 { |
| 178 | b.WriteByte(',') |
| 179 | } |
| 180 | b.WriteString(s[i : i+3]) |
| 181 | } |
| 182 | return b.String() |
| 183 | } |
| 184 | |
| 185 | // humanCompact formats large numbers with B/M/K suffix for compact display. |
| 186 | // ≥1B → "X.XXB"; ≥1M → "X.XXM"; ≥1K → "XK" or "X.XK"; else plain integer. |