forked from nsqio/nsq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
57 lines (52 loc) · 857 Bytes
/
template.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
package stringy
import (
"fmt"
)
func Commafy(i interface{}) string {
var n int64
switch i.(type) {
case int:
n = int64(i.(int))
case int64:
n = i.(int64)
case int32:
n = int64(i.(int32))
}
if n > 1000 {
r := n % 1000
n = n / 1000
return fmt.Sprintf("%s,%03d", Commafy(n), r)
}
return fmt.Sprintf("%d", n)
}
func FloatToPercent(i float64) string {
return fmt.Sprintf("%.0f", i*100.0)
}
func PercSuffix(i float64) string {
switch int(i*100) % 10 {
case 1:
return "st"
case 2:
return "nd"
case 3:
return "rd"
}
return "th"
}
func NanoSecondToHuman(v float64) string {
var suffix string
switch {
case v > 1000000000:
v /= 1000000000
suffix = "s"
case v > 1000000:
v /= 1000000
suffix = "ms"
case v > 1000:
v /= 1000
suffix = "us"
default:
suffix = "ns"
}
return fmt.Sprintf("%0.1f%s", v, suffix)
}