chore: add convert bytes to a human-readable string with the appropriate unit (bytes, MiB, or GiB)
This commit is contained in:
@@ -255,6 +255,18 @@ func CronNextTime(cronExpr string) time.Time {
|
||||
return next
|
||||
}
|
||||
|
||||
func BytesToMb(b uint64) uint64 {
|
||||
return b / 1024 / 1024
|
||||
// ConvertBytes converts bytes to a human-readable string with the appropriate unit (bytes, MiB, or GiB).
|
||||
func ConvertBytes(bytes uint64) string {
|
||||
const (
|
||||
MiB = 1024 * 1024
|
||||
GiB = MiB * 1024
|
||||
)
|
||||
switch {
|
||||
case bytes >= GiB:
|
||||
return fmt.Sprintf("%.2f GiB", float64(bytes)/float64(GiB))
|
||||
case bytes >= MiB:
|
||||
return fmt.Sprintf("%.2f MiB", float64(bytes)/float64(MiB))
|
||||
default:
|
||||
return fmt.Sprintf("%d bytes", bytes)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user