mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2025-11-19 23:50:25 +08:00
增加格式化数字函数
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
Reference in New Issue
Block a user