28 lines
445 B
Go
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])
|
|
}
|