Function HumanBytes for human-readable representation of numbers. #18

This commit is contained in:
Andrey Smirnov
2014-03-17 16:47:04 +04:00
parent 6a42aad322
commit 099806aa82
2 changed files with 43 additions and 0 deletions
+22
View File
@@ -0,0 +1,22 @@
package utils
import (
"fmt"
)
// Convert bytes to human readable string
func HumanBytes(i int64) (result string) {
switch {
case i > (512 * 1024 * 1024 * 1024):
result = fmt.Sprintf("%#.02f TiB", float64(i)/1024/1024/1024/1024)
case i > (512 * 1024 * 1024):
result = fmt.Sprintf("%#.02f GiB", float64(i)/1024/1024/1024)
case i > (512 * 1024):
result = fmt.Sprintf("%#.02f MiB", float64(i)/1024/1024)
case i > 512:
result = fmt.Sprintf("%#.02f KiB", float64(i)/1024)
default:
result = fmt.Sprintf("%d B", i)
}
return
}