diff --git a/internal/utils/numberutils/utils.go b/internal/utils/numberutils/utils.go index df16c20c..d2588969 100644 --- a/internal/utils/numberutils/utils.go +++ b/internal/utils/numberutils/utils.go @@ -75,7 +75,7 @@ func FormatCount(count int64) string { return fmt.Sprintf("%.1fB", float32(count)/1000/1000/1000) } -func FormatFloat(f interface{}, decimal int) string { +func FormatFloat(f any, decimal int) string { if f == nil { return "" } @@ -101,10 +101,29 @@ func FormatFloat(f interface{}, decimal int) string { return "" } -func FormatFloat2(f interface{}) string { +func FormatFloat2(f any) string { return FormatFloat(f, 2) } +// PadFloatZero 为浮点型数字字符串填充足够的0 +func PadFloatZero(s string, countZero int) string { + if countZero <= 0 { + return s + } + if len(s) == 0 { + s = "0" + } + var index = strings.Index(s, ".") + if index < 0 { + return s + "." + strings.Repeat("0", countZero) + } + var decimalLen = len(s) - 1 - index + if decimalLen < countZero { + return s + strings.Repeat("0", countZero-decimalLen) + } + return s +} + var decimalReg = regexp.MustCompile(`^(\d+\.\d+)([a-zA-Z]+)?$`) // TrimZeroSuffix 去除小数数字尾部多余的0 diff --git a/internal/utils/numberutils/utils_test.go b/internal/utils/numberutils/utils_test.go index 6065572a..c4a19318 100644 --- a/internal/utils/numberutils/utils_test.go +++ b/internal/utils/numberutils/utils_test.go @@ -4,6 +4,7 @@ package numberutils_test import ( "github.com/TeaOSLab/EdgeAdmin/internal/utils/numberutils" + "github.com/iwind/TeaGo/assert" "testing" ) @@ -49,6 +50,24 @@ func TestFormatFloat(t *testing.T) { t.Log(numberutils.FormatFloat(-221745.12, 2)) } +func TestFormatFloat2(t *testing.T) { + t.Log(numberutils.FormatFloat2(0)) + t.Log(numberutils.FormatFloat2(0.0)) + t.Log(numberutils.FormatFloat2(1.23456)) + t.Log(numberutils.FormatFloat2(1.0)) +} + +func TestPadFloatZero(t *testing.T) { + var a = assert.NewAssertion(t) + a.IsTrue(numberutils.PadFloatZero("1", 0) == "1") + a.IsTrue(numberutils.PadFloatZero("1", 2) == "1.00") + a.IsTrue(numberutils.PadFloatZero("1.1", 2) == "1.10") + a.IsTrue(numberutils.PadFloatZero("1.12", 2) == "1.12") + a.IsTrue(numberutils.PadFloatZero("1.123", 2) == "1.123") + a.IsTrue(numberutils.PadFloatZero("10000.123", 2) == "10000.123") + a.IsTrue(numberutils.PadFloatZero("", 2) == "0.00") +} + func TestTrimZeroSuffix(t *testing.T) { for _, s := range []string{ "1", diff --git a/internal/web/actions/default/clusters/cluster/nodes.go b/internal/web/actions/default/clusters/cluster/nodes.go index 7be989d7..76994c8e 100644 --- a/internal/web/actions/default/clusters/cluster/nodes.go +++ b/internal/web/actions/default/clusters/cluster/nodes.go @@ -225,7 +225,7 @@ func (this *NodesAction) RunGet(params struct { "memUsageText": fmt.Sprintf("%.2f%%", status.MemoryUsage*100), "trafficInBytes": status.TrafficInBytes, "trafficOutBytes": status.TrafficOutBytes, - "load1m": numberutils.FormatFloat2(status.Load1m), + "load1m": numberutils.PadFloatZero(numberutils.FormatFloat2(status.Load1m), 2), "countConnections": status.ConnectionCount, }, "cluster": maps.Map{ diff --git a/internal/web/actions/default/clusters/nodes.go b/internal/web/actions/default/clusters/nodes.go index 04a190e8..53b5b304 100644 --- a/internal/web/actions/default/clusters/nodes.go +++ b/internal/web/actions/default/clusters/nodes.go @@ -227,7 +227,7 @@ func (this *NodesAction) RunGet(params struct { "memUsageText": numberutils.FormatFloat2(status.MemoryUsage * 100), "trafficInBytes": status.TrafficInBytes, "trafficOutBytes": status.TrafficOutBytes, - "load1m": numberutils.FormatFloat2(status.Load1m), + "load1m": numberutils.PadFloatZero(numberutils.FormatFloat2(status.Load1m), 2), "countConnections": status.ConnectionCount, }, "cluster": maps.Map{ diff --git a/web/views/@default/clusters/cluster/nodes.html b/web/views/@default/clusters/cluster/nodes.html index c0dcb4d8..0a84bd45 100644 --- a/web/views/@default/clusters/cluster/nodes.html +++ b/web/views/@default/clusters/cluster/nodes.html @@ -139,7 +139,7 @@ - - {{node.status.load1m}} + {{node.status.load1m}} - diff --git a/web/views/@default/clusters/nodes.html b/web/views/@default/clusters/nodes.html index 99bcb340..4a6de9bb 100644 --- a/web/views/@default/clusters/nodes.html +++ b/web/views/@default/clusters/nodes.html @@ -134,7 +134,7 @@ - - {{node.status.load1m}} + {{node.status.load1m}} -