upgrade:float64 formatting method

This commit is contained in:
zhangwenjian committed 2020-08-20 05:33:37 +08:00
1 parent 7140ff8a3a
commit 2d11163ce5
2 files changed
+10 -3

No files matched your search

+1 -2
View File
@@ -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{
+9 -1
View File
@@ -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 是为了四舍五入,如果不希望这样去掉这个
}