From 9e2526405f3666ac1bfe0bc429413d21ac8210a9 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 27 Sep 2023 15:12:18 +0800 Subject: [PATCH] fix --- .../logic/getproductsteppricelogic.go | 7 ++++-- utils/format/number.go | 22 +++++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/server/product/internal/logic/getproductsteppricelogic.go b/server/product/internal/logic/getproductsteppricelogic.go index 9a96014c..bf5bc09d 100644 --- a/server/product/internal/logic/getproductsteppricelogic.go +++ b/server/product/internal/logic/getproductsteppricelogic.go @@ -108,14 +108,17 @@ func (l *GetProductStepPriceLogic) GetProductStepPrice(req *types.GetProductStep for rIndex, rangeInfo := range stepPrice.PriceRange { //最后一个 if rIndex+1 == rangeLen { + begin := format.NumToStringWithThousandthPercentile(fmt.Sprintf("%d", rangeInfo.StartQuantity)) stepRange = append(stepRange, map[string]interface{}{ - "range_description": fmt.Sprintf(">=%s Units", format.NumToStringWithThousandthPercentile(rangeInfo.StartQuantity)), + "range_description": fmt.Sprintf(">=%s Units", begin), "item_price": format.CentitoDollar(rangeInfo.Price, 3), }) break } + begin := format.NumToStringWithThousandthPercentile(fmt.Sprintf("%d", rangeInfo.StartQuantity)) + end := format.NumToStringWithThousandthPercentile(fmt.Sprintf("%d", rangeInfo.EndQuantity)) stepRange = append(stepRange, map[string]interface{}{ - "range_description": fmt.Sprintf("%s-%s Units", format.NumToStringWithThousandthPercentile(rangeInfo.StartQuantity), format.NumToStringWithThousandthPercentile(rangeInfo.EndQuantity)), + "range_description": fmt.Sprintf("%s-%s Units", begin, end), "item_price": format.CentitoDollar(rangeInfo.Price, 3), }) } diff --git a/utils/format/number.go b/utils/format/number.go index 8cbca6d3..d4912c5e 100644 --- a/utils/format/number.go +++ b/utils/format/number.go @@ -1,18 +1,31 @@ package format import ( - "fmt" + "log" + "strconv" "strings" ) // 数字变成带千分位的字符串 -func NumToStringWithThousandthPercentile(number int64) string { - s := fmt.Sprintf("%d", number) +func NumToStringWithThousandthPercentile(numberStr string) string { + if _, err := strconv.ParseFloat(numberStr, 64); err != nil { + log.Fatalln("is not a number") + return "" + } + sliceList := strings.Split(numberStr, ".") + s := sliceList[0] + f := "" + if len(sliceList) == 2 { + f = "." + sliceList[1] + } l := len(s) if l <= 3 { - return s + return s + f } r := l % 3 //前面第几位开始加入千分位 + if r == 0 { + r = 3 + } b := strings.Builder{} for i := 0; i < l; i++ { b.WriteString(string(s[i])) @@ -21,5 +34,6 @@ func NumToStringWithThousandthPercentile(number int64) string { r += 3 } } + b.WriteString(f) return b.String() }