convert.go 401 B

123456789101112131415161718192021
  1. package text
  2. import (
  3. "fmt"
  4. )
  5. // Human method returns results in human readable format.
  6. func Human(size int64) string {
  7. floatsize := float32(size)
  8. units := [...]string{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"}
  9. for _, unit := range units {
  10. if floatsize < 1024 {
  11. return fmt.Sprintf("%.1f %sB", floatsize, unit)
  12. }
  13. floatsize /= 1024
  14. }
  15. return fmt.Sprintf("%d%s", size, "B")
  16. }