-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitfmt.go
More file actions
36 lines (33 loc) · 804 Bytes
/
bitfmt.go
File metadata and controls
36 lines (33 loc) · 804 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
// bitfmt.go
package main
import (
"fmt"
)
//KiB and others are binary powers of byte. They are used to format values from
//big amount of bytes into human-readable values
const (
KiB float32 = 1024
MiB = 1024 * 1024
GiB = 1024 * 1024 * 1024
TiB = 1024 * 1024 * 1024 * 1024
PiB = 1024 * 1024 * 1024 * 1024 * 1024
)
//formats value in bytes into human-readable value in binary power of bytes.
func humanBytes(b float32) string {
if b > 10*PiB {
return fmt.Sprintf("%.0f PiB", b/PiB)
}
if b > 10*TiB {
return fmt.Sprintf("%.0f TiB", b/TiB)
}
if b > 10*GiB {
return fmt.Sprintf("%.0f GiB", b/GiB)
}
if b > 10*MiB {
return fmt.Sprintf("%.0f MiB", b/MiB)
}
if b > 10*KiB {
return fmt.Sprintf("%.0f KiB", b/KiB)
}
return fmt.Sprintf("%.0f B", b)
}