Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ var sizeGB = sizeMB * 1024
func Bytes2Size(num int64) string {
numStr := ""
unit := "B"
if num/int64(sizeGB) > 1 {
if num/int64(sizeGB) >= 1 {
numStr = fmt.Sprintf("%.2f", float64(num)/float64(sizeGB))
unit = "GB"
} else if num/int64(sizeMB) > 1 {
} else if num/int64(sizeMB) >= 1 {
numStr = fmt.Sprintf("%d", int(float64(num)/float64(sizeMB)))
unit = "MB"
} else if num/int64(sizeKB) > 1 {
} else if num/int64(sizeKB) >= 1 {
numStr = fmt.Sprintf("%d", int(float64(num)/float64(sizeKB)))
unit = "KB"
} else {
Expand Down
25 changes: 25 additions & 0 deletions common/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package common

import "testing"

func TestBytes2Size(t *testing.T) {
tests := []struct {
name string
input int64
want string
}{
{name: "bytes", input: 500, want: "500 B"},
{name: "kilobyte", input: 1024, want: "1 KB"},
{name: "kilobyteRounding", input: 1536, want: "1 KB"},
{name: "megabyte", input: 1024 * 1024, want: "1 MB"},
{name: "gigabyte", input: 1024 * 1024 * 1024, want: "1.00 GB"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Bytes2Size(tt.input); got != tt.want {
t.Fatalf("Bytes2Size(%d) = %s, want %s", tt.input, got, tt.want)
}
})
}
}