github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/utils/unit/unit.go (about) 1 package unit 2 3 // Unit . 4 type Unit int 5 6 const ( 7 //B . 8 B Unit = 0 9 //KB . 10 KB Unit = 1 11 //MB . 12 MB Unit = 2 13 //GB . 14 GB Unit = 3 15 //TB . 16 TB Unit = 4 17 //PB . 18 PB Unit = 5 19 ) 20 21 func (u Unit) String() string { 22 switch u { 23 case B: 24 return "B" 25 case KB: 26 return "KB" 27 case MB: 28 return "MB" 29 case GB: 30 return "GB" 31 case TB: 32 return "TB" 33 case PB: 34 return "PB" 35 default: 36 return "B" 37 } 38 } 39 40 // ReducedUnit . 41 func ReducedUnit(byte float64) (result float64, unit Unit) { 42 if byte >= 1125899906842624 { 43 return byte / 1125899906842624, PB //PB 44 } 45 if byte >= 1099511627776 { 46 return byte / 1099511627776, TB //TB 47 } 48 if byte >= 1073741824 { 49 return byte / 1073741824, GB //GB 50 } 51 if byte >= 1048576 { 52 return byte / 1048576, MB //MB 53 } 54 if byte >= 1024 { 55 return byte / 1024, KB //KB 56 } 57 return byte, B //B 58 }