-
Notifications
You must be signed in to change notification settings - Fork 0
/
ByteMath.go
114 lines (99 loc) · 2.16 KB
/
ByteMath.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package bytemath
/* based on gist: https://gist.github.com/anikitenko/b41206a49727b83a530142c76b1cb82d */
import (
"math"
"strconv"
)
type Suffix int
const (
B Suffix = iota
KB
MB
GB
TB
PB
)
type SuffixStrings struct {
Short string
Long string
}
var suffixStrings = map[Suffix]SuffixStrings{
B: {"B", "bytes"},
KB: {"KB", "kilobytes"},
MB: {"MB", "megabytes"},
GB: {"GB", "gigabytes"},
TB: {"TB", "terabytes"},
PB: {"PB", "petabytes"},
}
func GetSuffixString(sfx Suffix) SuffixStrings { return suffixStrings[sfx] }
func GetSuffixByString(s string) *Suffix {
for k, v := range suffixStrings {
if s == v.Short {
return &k
}
}
return nil
}
func ConvertToBytes(bytes float64, suffix Suffix) float64 {
for i := 0; i < getNumberOfLoops(suffix); i++ {
bytes = bytes * 1024.0
}
return bytes
}
func ConvertBytesToHumanReadable(sizeInBytes int64) string {
floatSize := float64(sizeInBytes)
base := math.Log(floatSize) / math.Log(1024)
getSize := round(math.Pow(1024, base-math.Floor(base)), .5, 2)
getSuffix := suffixStrings[Suffix(int(math.Floor(base)))]
return strconv.FormatFloat(getSize, 'f', -1, 64) + " " + getSuffix.Short
}
func ConvertBytes(iIn int64, s Suffix) float64 {
numLoops := getNumberOfLoops(s)
f := float64(iIn)
var multiplier float64 = 1024.00
for i := 0; i < numLoops; i++ {
f = f / multiplier
}
return f
}
func round(val float64, roundOn float64, places int) (newVal float64) {
var round float64
pow := math.Pow(10, float64(places))
digit := pow * val
_, div := math.Modf(digit)
if div >= roundOn {
round = math.Ceil(digit)
} else {
round = math.Floor(digit)
}
newVal = round / pow
return
}
func getNumberOfLoops(s Suffix) int {
i := 0
switch s {
case B:
i = 0
case KB:
i = 1
case MB:
i = 2
case GB:
i = 3
case TB:
i = 4
case PB:
i = 5
}
return i
}
func GetValueTable(bytes int64) map[Suffix]float64 {
valueTable := map[Suffix]float64{}
valueTable[B] = float64(bytes)
valueTable[KB] = ConvertBytes(bytes, KB)
valueTable[MB] = ConvertBytes(bytes, MB)
valueTable[GB] = ConvertBytes(bytes, GB)
valueTable[TB] = ConvertBytes(bytes, TB)
valueTable[PB] = ConvertBytes(bytes, PB)
return valueTable
}