xmtop/utils/bytes.go

48 lines
774 B
Go
Raw Normal View History

2018-02-19 15:25:02 +08:00
package utils
import (
"math"
)
2018-08-09 13:30:24 +08:00
var (
KB = uint64(math.Pow(2, 10))
MB = uint64(math.Pow(2, 20))
GB = uint64(math.Pow(2, 30))
TB = uint64(math.Pow(2, 40))
)
func CelsiusToFahrenheit(c int) int {
return c*9/5 + 32
}
2018-02-23 15:58:48 +08:00
func BytesToKB(b uint64) float64 {
2018-08-09 13:30:24 +08:00
return float64(b) / float64(KB)
2018-02-19 15:25:02 +08:00
}
2018-02-23 15:58:48 +08:00
func BytesToMB(b uint64) float64 {
2018-08-09 13:30:24 +08:00
return float64(b) / float64(MB)
2018-02-19 15:25:02 +08:00
}
2018-02-23 15:58:48 +08:00
func BytesToGB(b uint64) float64 {
2018-08-09 13:30:24 +08:00
return float64(b) / float64(GB)
}
func BytesToTB(b uint64) float64 {
return float64(b) / float64(TB)
2018-02-19 15:25:02 +08:00
}
2018-04-11 11:08:02 +08:00
2018-05-22 07:02:31 +08:00
func ConvertBytes(b uint64) (float64, string) {
2018-08-09 13:30:24 +08:00
switch {
case b < KB:
2018-05-22 07:02:31 +08:00
return float64(b), "B"
2018-08-09 13:30:24 +08:00
case b < MB:
return BytesToKB(b), "KB"
case b < GB:
return BytesToMB(b), "MB"
case b < TB:
return BytesToGB(b), "GB"
default:
return BytesToTB(b), "TB"
2018-05-22 07:02:31 +08:00
}
}