-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit.go
59 lines (49 loc) · 985 Bytes
/
unit.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
package number
import (
"fmt"
"math"
)
type Unit int64
// 1k = 1000 thousand/kilo
// 1M = 1000_000 million
// 1G = 1000_000_000 billion
const (
A Unit = 1
K = 1000 * A
W = 10 * K
M = 1000 * K
G = 1000 * M
)
// Format get the format of byte size
func (b Unit) Format() (float64, string) {
if b == 0 {
return 0, ""
}
var sizes = []string{"", "K", "M", "G"}
var i = math.Floor(math.Log10(float64(b)) / math.Log10(1000))
//the max data unit is to `G`
var sizesLen = float64(len(sizes))
if i > sizesLen {
i = sizesLen - 1
}
return float64(b) / math.Pow(1000, i), sizes[int(i)]
}
func (b Unit) String() string {
v, unit := b.Format()
return fmt.Sprintf("%.4f %v", v, unit)
}
func (b Unit) Unit() float64 {
return float64(b)
}
func (b Unit) K() float64 {
return float64(b / K)
}
func (b Unit) W() float64 {
return float64(b / W)
}
func (b Unit) M() float64 {
return float64(b / M)
}
func (b Unit) G() float64 {
return float64(b / G)
}