github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/humannumbers/bytes.go (about) 1 package humannumbers 2 3 import "fmt" 4 5 // Exportable byte denominations 6 const ( 7 _ = iota 8 KB = 1 << (10 * iota) 9 MB 10 GB 11 TB 12 PB 13 EB 14 ZB 15 YB 16 ) 17 18 // Byte denominations as floats to make division cheaper (less casting at runtime) 19 const ( 20 _ = iota 21 fKB float64 = 1 << (10 * iota) 22 fMB 23 fGB 24 fTB 25 fPB 26 fEB 27 //fZB 28 //fYB 29 ) 30 31 // Bytes converts n bytes into a human readable format 32 func Bytes(size uint64) (human string) { 33 switch { 34 //case size > YB*2: 35 // human = fmt.Sprintf("%.8f YB", float64(size)/fYB) 36 //case size > ZB*2: 37 // human = fmt.Sprintf("%.8f ZB", float64(size)/fZB) 38 case size > EB*2: 39 human = fmt.Sprintf("%.6f EB", float64(size)/fEB) 40 case size > PB*2: 41 human = fmt.Sprintf("%.6f PB", float64(size)/fPB) 42 case size > TB*2: 43 human = fmt.Sprintf("%.4f TB", float64(size)/fTB) 44 case size > GB*2: 45 human = fmt.Sprintf("%.4f GB", float64(size)/fGB) 46 case size > MB*2: 47 human = fmt.Sprintf("%.2f MB", float64(size)/fMB) 48 case size > KB*2: 49 human = fmt.Sprintf("%.2f KB", float64(size)/fKB) 50 default: 51 human = fmt.Sprintf("%0d bytes", size) 52 } 53 return 54 }