bytes.go 445 B

1234567891011121314151617181920212223
  1. package format
  2. import "fmt"
  3. const (
  4. Byte = 1
  5. KiloByte = Byte * 1000
  6. MegaByte = KiloByte * 1000
  7. GigaByte = MegaByte * 1000
  8. )
  9. func HumanBytes(b int64) string {
  10. switch {
  11. case b > GigaByte:
  12. return fmt.Sprintf("%.1f GB", float64(b)/GigaByte)
  13. case b > MegaByte:
  14. return fmt.Sprintf("%.1f MB", float64(b)/MegaByte)
  15. case b > KiloByte:
  16. return fmt.Sprintf("%.1f KB", float64(b)/KiloByte)
  17. default:
  18. return fmt.Sprintf("%d B", b)
  19. }
  20. }