/
byte_conversions.go
77 lines (64 loc) · 2.25 KB
/
byte_conversions.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
/**
This file is part of s3explorer.
s3explorer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
s3explorer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with s3explorer. If not, see <https://www.gnu.org/licenses/>.
**/
package main
import (
"log"
"math"
"strconv"
)
func RoundUp(input float64, places int) (newVal float64) {
var round float64
pow := math.Pow(10, float64(places))
digit := pow * input
round = math.Ceil(digit)
newVal = round / pow
return
}
func ByteFormat(inputNum float64, precision int) string {
log.Printf("Formatting input %v to human readable with precision %v\n", inputNum, precision)
if precision <= 0 {
precision = 1
}
var unit string
var returnVal float64
if inputNum >= 1000000000000000000000000 {
returnVal = RoundUp((inputNum / 1208925819614629174706176), precision)
unit = " YB" // yottabyte
} else if inputNum >= 1000000000000000000000 {
returnVal = RoundUp((inputNum / 1180591620717411303424), precision)
unit = " ZB" // zettabyte
} else if inputNum >= 10000000000000000000 {
returnVal = RoundUp((inputNum / 1152921504606846976), precision)
unit = " EB" // exabyte
} else if inputNum >= 1000000000000000 {
returnVal = RoundUp((inputNum / 1125899906842624), precision)
unit = " PB" // petabyte
} else if inputNum >= 1000000000000 {
returnVal = RoundUp((inputNum / 1099511627776), precision)
unit = " TB" // terrabyte
} else if inputNum >= 1000000000 {
returnVal = RoundUp((inputNum / 1073741824), precision)
unit = " GB" // gigabyte
} else if inputNum >= 1000000 {
returnVal = RoundUp((inputNum / 1048576), precision)
unit = " MB" // megabyte
} else if inputNum >= 1000 {
returnVal = RoundUp((inputNum / 1024), precision)
unit = " KB" // kilobyte
} else {
returnVal = inputNum
unit = " B" // byte
}
return strconv.FormatFloat(returnVal, 'f', precision, 64) + unit
}