1
0

Utility function to convert size ib bytes to its corresponding biggest unit.

This commit is contained in:
Alexander Andreev 2022-02-06 03:00:32 +04:00
parent bf4f41c85e
commit 71c3eb56b6
Signed by: Arav
GPG Key ID: 1327FE8A374CC86F

16
pkg/utils/filesize.go Normal file
View File

@ -0,0 +1,16 @@
package utils
import "fmt"
var sizeSuffixes = [...]string{"B", "KiB", "MiB", "GiB"}
// ConvertFileSize converts size in bytes down to biggest units it represents.
// Returns converted size, unit and, a concatenation of size and unit
func ConvertFileSize(size int64) (int64, string, string) {
var idx int
for idx = 0; size >= 1024; size /= 1024 {
}
return size, sizeSuffixes[idx], fmt.Sprint(size, sizeSuffixes[idx])
}