summaryrefslogtreecommitdiff
path: root/internal/textutil/human.go
blob: 21907c379da3bf57c5d948057db41001736754ac (plain)
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
package textutil

import "fmt"

// HumanBytes renders n in a short human-friendly form using base-1000 units.
// Examples: 999 -> 999B, 1200 -> 1.2k, 1540000 -> 1.5M
func HumanBytes(n int64) string {
	if n < 1000 {
		return fmt.Sprintf("%dB", n)
	}
	const unit = 1000.0
	v := float64(n)
	suffix := []string{"k", "M", "G", "T"}
	i := 0
	for v >= unit && i < len(suffix)-1 {
		v /= unit
		i++
	}
	s := fmt.Sprintf("%.1f%s", v, suffix[i])
	// Strip trailing ".0"
	if len(s) >= 3 && s[len(s)-2:] == ".0" {
		s = fmt.Sprintf("%d%s", int(v), suffix[i])
	}
	return s
}