This commit is contained in:
jiangyong 2026-04-17 16:55:38 +08:00
parent 17bf4aaf99
commit 8c3c30688f
1 changed files with 28 additions and 0 deletions

28
util.go
View File

@ -23,6 +23,34 @@ func FormatMoney(number int64) string {
return strconv.FormatInt(int64(num1), 10)
}
func FormatCNY(amountStr string) string {
amount, err := strconv.ParseFloat(amountStr, 64)
if err != nil {
return ""
}
var value float64
var unit string
switch {
case amount < 1000:
value = amount
unit = ""
case amount < 10000:
value = amount / 1000
unit = "k"
default:
value = amount / 10000
unit = "w"
}
// 去除末尾的零
s := strconv.FormatFloat(value, 'f', 2, 64)
s = strings.TrimRight(s, "0")
s = strings.TrimRight(s, ".")
return s + unit
}
func FormatFloat(f float64) string {
if int64(f*100)%100 == 0 {
return fmt.Sprintf("%d", int64(f))