visable.go 376 B

123456789101112131415161718192021
  1. package tests
  2. import "fmt"
  3. func ReadableBytes(l int) string {
  4. // convert l bytes to a readable string
  5. if l < 1024 {
  6. return fmt.Sprintf("%d B", l)
  7. }
  8. if l < 1024*1024 {
  9. return fmt.Sprintf("%.2f KB", float64(l)/1024)
  10. }
  11. if l < 1024*1024*1024 {
  12. return fmt.Sprintf("%.2f MB", float64(l)/(1024*1024))
  13. }
  14. return fmt.Sprintf("%.2f GB", float64(l)/(1024*1024*1024))
  15. }