-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.go
More file actions
41 lines (38 loc) · 753 Bytes
/
memory.go
File metadata and controls
41 lines (38 loc) · 753 Bytes
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
package gotools
import (
"fmt"
)
const (
B = iota // ignore first value by assigning to blank identifier
KB = 1 << (10 * iota)
MB
GB
TB
PB
EB
ZB
YB
)
// MemoryFormat retun memory size (in Bytes) in verbalize form,
// for example 2.01MB.
func MemoryFormat(x float64) string {
switch {
case x >= YB:
return fmt.Sprintf("%.2fYB", x/YB)
case x >= ZB:
return fmt.Sprintf("%.2fZB", x/ZB)
case x >= EB:
return fmt.Sprintf("%.2fEB", x/EB)
case x >= PB:
return fmt.Sprintf("%.2fPB", x/PB)
case x >= TB:
return fmt.Sprintf("%.2fTB", x/TB)
case x >= GB:
return fmt.Sprintf("%.2fGB", x/GB)
case x >= MB:
return fmt.Sprintf("%.2fMB", x/MB)
case x >= KB:
return fmt.Sprintf("%.2fKB", x/KB)
}
return fmt.Sprintf("%.0fB", x)
}