-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutil.go
More file actions
38 lines (33 loc) · 701 Bytes
/
Copy pathutil.go
File metadata and controls
38 lines (33 loc) · 701 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
package main
import (
"fmt"
"strings"
)
const (
BYTE = 1.0
KILOBYTE = 1024 * BYTE
MEGABYTE = 1024 * KILOBYTE
GIGABYTE = 1024 * MEGABYTE
)
func formatBytes(numberOfBytes int) string {
unit := ""
value := float32(numberOfBytes)
switch {
case numberOfBytes >= GIGABYTE:
unit = "G"
value = value / GIGABYTE
case numberOfBytes >= MEGABYTE:
unit = "M"
value = value / MEGABYTE
case numberOfBytes >= KILOBYTE:
unit = "K"
value = value / KILOBYTE
case numberOfBytes >= BYTE:
unit = "B"
case numberOfBytes == 0:
return "0"
}
stringValue := fmt.Sprintf("%.1f", value)
stringValue = strings.TrimSuffix(stringValue, ".0")
return fmt.Sprintf("%s%s", stringValue, unit)
}