/
t_math.go
39 lines (36 loc) · 885 Bytes
/
t_math.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package tools
import (
"strconv"
)
//单位转换: 传入值的单位(B)
func MathChangeUnit(unit int64) string {
m := int64(1024)
switch {
case unit < m:
return Int64ToStr(unit) + "B"
case m <= unit && unit < m*m:
tmp := float64(unit) / 1024
return Float64ToStr(tmp) + "KB"
case m*m <= unit && unit < m*m*m:
tmp := float64(unit) / 1024 / 1024
return Float64ToStr(tmp) + "MB"
case m*m*m <= unit && unit < m*m*m*m:
tmp := float64(unit) / 1024 / 1024 / 1024
return Float64ToStr(tmp) + "GB"
default:
tmp := float64(unit) / 1024 / 1024 / 1024 / 1024
return Float64ToStr(tmp) + "TB"
}
}
func Int64ToStr(x int64) string {
return strconv.FormatInt(x, 10)
}
func Float64ToStr(x float64) string {
return strconv.FormatFloat(x, 'f', 2, 64)
}
func IntToStr(x int) string {
return strconv.Itoa(x)
}
func StrToInt(x string) (int, error) {
return strconv.Atoi(x)
}