add format bytes

This commit is contained in:
Michael Yang
2023-10-11 10:55:07 -07:00
parent aca2d65b82
commit b599946b74
4 changed files with 36 additions and 20 deletions

16
format/bytes.go Normal file
View File

@@ -0,0 +1,16 @@
package format
import "fmt"
func HumanBytes(b int64) string {
switch {
case b > 1000*1000*1000:
return fmt.Sprintf("%d GB", b/1000/1000/1000)
case b > 1000*1000:
return fmt.Sprintf("%d MB", b/1000/1000)
case b > 1000:
return fmt.Sprintf("%d KB", b/1000)
default:
return fmt.Sprintf("%d B", b)
}
}