23 lines
582 B
Go
23 lines
582 B
Go
package files
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
var sizeSuffixes = [...]string{"B", "KiB", "MiB", "GiB", "TiB"}
|
|
|
|
// convertFileSize converts size in bytes down to biggest units it represents.
|
|
// Returns a converted size string and a unit idx
|
|
func convertFileSize(size int64) (string, string) {
|
|
var idx uint8
|
|
var fSize float64 = float64(size)
|
|
for idx = 0; fSize >= 1024; fSize /= 1024 {
|
|
idx++
|
|
}
|
|
fSizeStr := strconv.FormatFloat(fSize, 'f', 3, 64)
|
|
fSizeStr = strings.TrimRight(fSizeStr, "0")
|
|
fSizeStr = strings.TrimSuffix(fSizeStr, ".")
|
|
return fSizeStr, sizeSuffixes[idx]
|
|
}
|