Utility function to convert size ib bytes to its corresponding biggest unit.
This commit is contained in:
parent
bf4f41c85e
commit
71c3eb56b6
16
pkg/utils/filesize.go
Normal file
16
pkg/utils/filesize.go
Normal 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])
|
||||
}
|
Loading…
Reference in New Issue
Block a user