convert.go 393 B

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