humanInt formats a non-negative integer with commas so a context window like 262144 reads as "262,144" rather than a wall of digits.
(n int)
| 69 | // humanInt formats a non-negative integer with commas so a context window |
| 70 | // like 262144 reads as "262,144" rather than a wall of digits. |
| 71 | func humanInt(n int) string { |
| 72 | s := strconv.Itoa(n) |
| 73 | if len(s) <= 3 { |
| 74 | return s |
| 75 | } |
| 76 | var b strings.Builder |
| 77 | b.Grow(len(s) + (len(s)-1)/3) |
| 78 | head := len(s) % 3 |
| 79 | if head == 0 { |
| 80 | head = 3 |
| 81 | } |
| 82 | b.WriteString(s[:head]) |
| 83 | for i := head; i < len(s); i += 3 { |
| 84 | b.WriteByte(',') |
| 85 | b.WriteString(s[i : i+3]) |
| 86 | } |
| 87 | return b.String() |
| 88 | } |