You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
712 B
37 lines
712 B
|
8 years ago
|
package rest
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"strconv"
|
||
|
|
)
|
||
|
|
|
||
|
|
//把一个大小转变成方便读的格式
|
||
|
|
//human readable file size
|
||
|
|
func HumanFileSize(bytes int64, si bool) string {
|
||
|
|
var thresh int64 = 1000
|
||
|
|
if si {
|
||
|
|
thresh = 1024
|
||
|
|
}
|
||
|
|
if bytes < 0 {
|
||
|
|
bytes = 0
|
||
|
|
}
|
||
|
|
if bytes < thresh {
|
||
|
|
return fmt.Sprintf("%dB", bytes)
|
||
|
|
}
|
||
|
|
var units = []string{"kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
|
||
|
|
if si {
|
||
|
|
units = []string{"KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}
|
||
|
|
}
|
||
|
|
var u = 0
|
||
|
|
var tmp = float64(bytes)
|
||
|
|
var standard = float64(thresh)
|
||
|
|
for tmp >= standard && u < len(units)-1 {
|
||
|
|
tmp /= float64(standard)
|
||
|
|
u++
|
||
|
|
}
|
||
|
|
|
||
|
|
numStr := strconv.FormatFloat(tmp, 'f', 1, 64)
|
||
|
|
|
||
|
|
return fmt.Sprintf("%s%s", numStr, units[u])
|
||
|
|
}
|