diff --git a/internal/utils/numberutils/utils.go b/internal/utils/numberutils/utils.go index 87f4b9e5..26da1793 100644 --- a/internal/utils/numberutils/utils.go +++ b/internal/utils/numberutils/utils.go @@ -82,6 +82,30 @@ func FormatFloat(f interface{}, decimal int) string { switch x := f.(type) { case float32, float64: var s = fmt.Sprintf("%."+types.String(decimal)+"f", x) + + // 分隔 + var dotIndex = strings.Index(s, ".") + if dotIndex > 0 { + var d = s[:dotIndex] + var l = len(d) + if l > 3 { + var f2 = s[dotIndex:] // 包含点(.) + var pieces = l / 3 + var commIndex = l - pieces*3 + var d2 = "" + if commIndex > 0 { + d2 = d[:commIndex] + ", " + } + for i := 0; i < pieces; i++ { + d2 += d[commIndex + i*3 : commIndex + i*3+3] + if i != pieces-1 { + d2 += ", " + } + } + return d2 + f2 + } + } + return s case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: return types.String(x) diff --git a/internal/utils/numberutils/utils_test.go b/internal/utils/numberutils/utils_test.go index 68041019..e808a29a 100644 --- a/internal/utils/numberutils/utils_test.go +++ b/internal/utils/numberutils/utils_test.go @@ -33,6 +33,14 @@ func TestFormatFloat(t *testing.T) { t.Log(numberutils.FormatFloat(100.23456, 2)) t.Log(numberutils.FormatFloat(100.000023, 2)) t.Log(numberutils.FormatFloat(100.012, 2)) + t.Log(numberutils.FormatFloat(123.012, 2)) + t.Log(numberutils.FormatFloat(1234.012, 2)) + t.Log(numberutils.FormatFloat(12345.012, 2)) + t.Log(numberutils.FormatFloat(123456.012, 2)) + t.Log(numberutils.FormatFloat(1234567.012, 2)) + t.Log(numberutils.FormatFloat(12345678.012, 2)) + t.Log(numberutils.FormatFloat(123456789.012, 2)) + t.Log(numberutils.FormatFloat(1234567890.012, 2)) } func TestTrimZeroSuffix(t *testing.T) {