forked from zofuthan/usage-reporting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatting.go
71 lines (59 loc) · 1.13 KB
/
formatting.go
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"bytes"
"fmt"
"strings"
)
func number(isBinary bool, v float64) string {
if isBinary {
return binary(v)
} else {
return metric(v)
}
}
type prefix struct {
Prefix string
Multiplier float64
}
var metricPrefixes = []prefix{
{"G", 1e9},
{"M", 1e6},
{"k", 1e3},
}
var binaryPrefixes = []prefix{
{"Gi", 1 << 30},
{"Mi", 1 << 20},
{"Ki", 1 << 10},
}
func metric(v float64) string {
return withPrefix(v, metricPrefixes)
}
func binary(v float64) string {
return withPrefix(v, binaryPrefixes)
}
func withPrefix(v float64, ps []prefix) string {
for _, p := range ps {
if v >= p.Multiplier {
return fmt.Sprintf("%.1f %s", v/p.Multiplier, p.Prefix)
}
}
return fmt.Sprint(v)
}
// commatize returns a number with sep as thousands separators. Handles
// integers and plain floats.
func commatize(sep, s string) string {
var b bytes.Buffer
fs := strings.SplitN(s, ".", 2)
l := len(fs[0])
for i := range fs[0] {
b.Write([]byte{s[i]})
if i < l-1 && (l-i)%3 == 1 {
b.WriteString(sep)
}
}
if len(fs) > 1 && len(fs[1]) > 0 {
b.WriteString(".")
b.WriteString(fs[1])
}
return b.String()
}