From bf500fe1a4b9bbdea1f55be327bab72d9e06f84d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Mon, 3 Jan 2022 12:04:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=A0=BC=E5=BC=8F=E5=8C=96?= =?UTF-8?q?=E6=95=B0=E5=AD=97=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/utils/numberutils/utils.go | 14 ++++++++++++++ internal/utils/numberutils/utils_test.go | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/internal/utils/numberutils/utils.go b/internal/utils/numberutils/utils.go index 7b0c09e1..385516cc 100644 --- a/internal/utils/numberutils/utils.go +++ b/internal/utils/numberutils/utils.go @@ -2,6 +2,7 @@ package numberutils import ( "fmt" + "github.com/iwind/TeaGo/types" "strconv" ) @@ -40,3 +41,16 @@ func FormatBytes(bytes int64) string { return fmt.Sprintf("%.2fEB", float64(bytes)/float64(Pow1024(6))) } } + +func FormatCount(count int64) string { + if count < 1000 { + return types.String(count) + } + if count < 1000 * 1000 { + return fmt.Sprintf("%.1fK", float32(count)/1000) + } + if count < 1000 * 1000 * 1000{ + return fmt.Sprintf("%.1fM", float32(count)/1000/1000) + } + return fmt.Sprintf("%.1fB", float32(count)/1000/1000/1000) +} diff --git a/internal/utils/numberutils/utils_test.go b/internal/utils/numberutils/utils_test.go index c71f1033..bae16434 100644 --- a/internal/utils/numberutils/utils_test.go +++ b/internal/utils/numberutils/utils_test.go @@ -14,3 +14,13 @@ func TestFormatBytes(t *testing.T) { t.Log(FormatBytes(1_000_000_000_000_000_000)) t.Log(FormatBytes(9_000_000_000_000_000_000)) } + +func TestFormatCount(t *testing.T) { + t.Log(FormatCount(1)) + t.Log(FormatCount(1000)) + t.Log(FormatCount(1500)) + t.Log(FormatCount(1_000_000)) + t.Log(FormatCount(1_500_000)) + t.Log(FormatCount(1_000_000_000)) + t.Log(FormatCount(1_500_000_000)) +} \ No newline at end of file