forked from FeatureBaseDB/pdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytes.go
53 lines (46 loc) · 1.06 KB
/
bytes.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
package pdk
// This code adapted from https://github.com/cloudfoundry/bytefmt (Apache V2)
import (
"fmt"
"strings"
)
const (
BYTE = 1.0
KILOBYTE = 1024 * BYTE
MEGABYTE = 1024 * KILOBYTE
GIGABYTE = 1024 * MEGABYTE
TERABYTE = 1024 * GIGABYTE
)
type Bytes uint64
// Returns a human-readable byte string of the form 10M, 12.5K, and so forth. The following units are available:
// T: Terabyte
// G: Gigabyte
// M: Megabyte
// K: Kilobyte
// B: Byte
// The unit that results in the smallest number greater than or equal to 1 is always chosen.
func (b Bytes) String() string {
unit := ""
value := float32(b)
switch {
case b >= TERABYTE:
unit = "T"
value = value / TERABYTE
case b >= GIGABYTE:
unit = "G"
value = value / GIGABYTE
case b >= MEGABYTE:
unit = "M"
value = value / MEGABYTE
case b >= KILOBYTE:
unit = "K"
value = value / KILOBYTE
case b >= BYTE:
unit = "B"
case b == 0:
return "0"
}
stringValue := fmt.Sprintf("%.1f", value)
stringValue = strings.TrimSuffix(stringValue, ".0")
return fmt.Sprintf("%s%s", stringValue, unit)
}