|
- package utils
-
- import (
- "fmt"
- "math"
- "strings"
- )
-
- func CouponFormat(data string) string {
- switch data {
- case "0.00", "0", "":
- return ""
- default:
- return Int64ToStr(FloatToInt64(StrToFloat64(data)))
- }
- }
- func CommissionFormat(data string) string {
- if data != "" && data != "0" {
- return data
- }
-
- return ""
- }
-
- func HideString(src string, hLen int) string {
- str := []rune(src)
- if hLen == 0 {
- hLen = 4
- }
- hideStr := ""
- for i := 0; i < hLen; i++ {
- hideStr += "*"
- }
- hideLen := len(str) / 2
- showLen := len(str) - hideLen
- if hideLen == 0 || showLen == 0 {
- return hideStr
- }
- subLen := showLen / 2
- if subLen == 0 {
- return string(str[:showLen]) + hideStr
- }
- s := string(str[:subLen])
- s += hideStr
- s += string(str[len(str)-subLen:])
- return s
- }
-
- // SaleCountFormat is 格式化销量
- func SaleCountFormat(s string) string {
- return s + "已售"
- }
- func SaleCountFormat1(s string) string {
- s = strings.ReplaceAll(s, "+", "")
- if strings.Contains(s, "万") {
- s = strings.ReplaceAll(s, "万", "")
- s = Float64ToStrByPrec(StrToFloat64(s)*10000, 0)
- }
- ex := strings.Split(s, ".")
- if len(ex) == 2 && StrToInt(ex[1]) == 0 {
- s = ex[0]
- }
- if StrToInt(s) > 0 {
- s = Comm(s)
- return s
- }
- return "0"
- }
- func Comm(s string) string {
- ex := strings.Split(s, ".")
- if len(ex) == 2 && StrToInt(ex[1]) == 0 {
- s = ex[0]
- }
- if StrToInt(s) >= 10000 {
- num := FloatFormat(StrToFloat64(s)/10000, 2)
- numStr := Float64ToStr(num)
- ex := strings.Split(numStr, ".")
- if len(ex) == 2 {
- if StrToFloat64(ex[1]) == 0 {
- numStr = ex[0]
- } else {
- val := Float64ToStrByPrec(StrToFloat64(ex[1]), 0)
- keyMax := 0
- for i := 0; i < len(val); i++ {
- ch := string(val[i])
- fmt.Println(StrToInt(ch))
- if StrToInt(ch) > 0 {
- keyMax = i
- }
- }
- valNew := val[0 : keyMax+1]
- numStr = ex[0] + "." + strings.ReplaceAll(ex[1], val, valNew)
- }
- }
- s = numStr + "万"
- }
- return s
- }
-
- // 小数格式化
- func FloatFormat(f float64, i int) float64 {
- if i > 14 {
- return f
- }
- p := math.Pow10(i)
- return float64(int64((f+0.000000000000009)*p)) / p
- }
|