yay/pkg/text/convert.go

22 lines
401 B
Go
Raw Normal View History

package text
2020-07-10 00:36:45 +00:00
import (
"fmt"
)
// Human method returns results in human readable format.
func Human(size int64) string {
floatsize := float32(size)
2021-08-11 18:13:28 +00:00
units := [...]string{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"}
for _, unit := range units {
if floatsize < 1024 {
return fmt.Sprintf("%.1f %sB", floatsize, unit)
}
2021-08-11 18:13:28 +00:00
floatsize /= 1024
}
2021-08-11 18:13:28 +00:00
return fmt.Sprintf("%d%s", size, "B")
}