binaryimage/misc.go
2025-02-28 23:34:15 +01:00

28 lines
445 B
Go

package main
import "fmt"
func fileSizeToString(fileSize int) string {
binarySizes := []string{
"B",
"kiB",
"MiB",
"GiB",
"TiB",
"PiB",
"EiB",
"ZiB",
"YiB",
"RiB",
"QiB",
}
smallerNumber := float64(fileSize)
size := 0
binarySizesCount := len(binarySizes)
for smallerNumber >= 1024 || size >= binarySizesCount {
smallerNumber /= 1024
size++
}
return fmt.Sprintf("%.2f %v", smallerNumber, binarySizes[size])
}