diff --git a/apis/monitor/server.go b/apis/monitor/server.go index 852df67..5bfc656 100644 --- a/apis/monitor/server.go +++ b/apis/monitor/server.go @@ -1,7 +1,6 @@ package monitor import ( - "fmt" "github.com/gin-gonic/gin" "github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/disk" @@ -56,7 +55,7 @@ func ServerInfo(c *gin.Context) { cpuDic := make(map[string]interface{}, 0) cpuDic["cpuInfo"],_ = cpu.Info() percent,_ := cpu.Percent(0,false) - cpuDic["Percent"] = fmt.Sprintf("%.2f",percent[0]) + cpuDic["Percent"] = tools.Round(percent[0],2) cpuDic["cpuNum"], _ = cpu.Counts(false) app.Custum(c, gin.H{ diff --git a/tools/float64.go b/tools/float64.go index 5059bd1..583ff9f 100644 --- a/tools/float64.go +++ b/tools/float64.go @@ -1,7 +1,15 @@ package tools -import "strconv" +import ( + "math" + "strconv" +) func Float64ToString(e float64) string { return strconv.FormatFloat(e, 'E', -1, 64) } + +func Round(f float64, n int) float64 { + pow10_n := math.Pow10(n) + return math.Trunc((f+0.5/pow10_n)*pow10_n) / pow10_n // TODO +0.5 是为了四舍五入,如果不希望这样去掉这个 +}