From 584cb5d5310ccfb68a6ab74a2a00c4f407c8b2b9 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 21 Sep 2023 18:21:31 +0800 Subject: [PATCH 1/2] fix --- .../internal/logic/calculatecartpricelogic.go | 6 +++--- server/shopping-cart/internal/logic/getcartslogic.go | 4 ++-- utils/format/price.go | 12 ++++-------- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/server/shopping-cart/internal/logic/calculatecartpricelogic.go b/server/shopping-cart/internal/logic/calculatecartpricelogic.go index 81772b57..4dbe0ed4 100644 --- a/server/shopping-cart/internal/logic/calculatecartpricelogic.go +++ b/server/shopping-cart/internal/logic/calculatecartpricelogic.go @@ -129,8 +129,8 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri } calculateResultList = append(calculateResultList, types.CalculateResultItem{ CartId: cart.Id, - ItemPrice: fmt.Sprintf("%.3f", format.CentitoDollar(itemPrice)), - TotalPrice: fmt.Sprintf("%.3f", format.CentitoDollar(totalPrice)), + ItemPrice: format.CentitoDollar(itemPrice, 3), + TotalPrice: format.CentitoDollar(totalPrice, 2), }) updData := &gmodel.FsShoppingCart{ PurchaseQuantity: &reqPurchaseQuantity, @@ -152,7 +152,7 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri return resp.SetStatusWithMessage(basic.CodeDbSqlErr, err.Error()) } return resp.SetStatusWithMessage(basic.CodeOK, "success", types.CalculateCartPriceRsp{ - SubTotalPrice: fmt.Sprintf("%.3f", format.CentitoDollar(subTotalPrice)), + SubTotalPrice: format.CentitoDollar(subTotalPrice, 2), CalculateResultList: calculateResultList, }) } diff --git a/server/shopping-cart/internal/logic/getcartslogic.go b/server/shopping-cart/internal/logic/getcartslogic.go index 290740a0..35ba6f08 100644 --- a/server/shopping-cart/internal/logic/getcartslogic.go +++ b/server/shopping-cart/internal/logic/getcartslogic.go @@ -173,8 +173,8 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo FittingId: *cart.FittingId, FittingName: fittingName, }, - ItemPrice: fmt.Sprintf("%.3f", format.CentitoDollar(itemPrice)), - TotalPrice: fmt.Sprintf("%.3f", format.CentitoDollar(totalPrice)), + ItemPrice: format.CentitoDollar(itemPrice, 3), + TotalPrice: format.CentitoDollar(totalPrice, 2), DiyInformation: types.DiyInformation{ Phone: snapShot.UserDiyInformation.Phone, Address: snapShot.UserDiyInformation.Address, diff --git a/utils/format/price.go b/utils/format/price.go index 9af4a35e..23603452 100644 --- a/utils/format/price.go +++ b/utils/format/price.go @@ -2,19 +2,15 @@ package format import ( "fmt" - "strconv" ) // 厘转美元 -func CentitoDollar(price int64, remainFloatPoint ...uint) float64 { - s := "%.3f" +func CentitoDollar(price int64, remainFloatPoint ...uint) string { + s := "%0.3f" if len(remainFloatPoint) > 0 { - s = fmt.Sprintf("%%.%df", remainFloatPoint[0]) + s = fmt.Sprintf("%%0.%df", remainFloatPoint[0]) } - fmt.Println(s) - str := fmt.Sprintf(s, float64(price)/float64(1000)) - dollar, _ := strconv.ParseFloat(str, 64) - return dollar + return fmt.Sprintf(s, float64(price)/float64(1000)) } // 厘转美元 From f767af9a7f48407e01feb373bfa904c78cd6aaac Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 21 Sep 2023 18:22:55 +0800 Subject: [PATCH 2/2] fix --- utils/format/price.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/format/price.go b/utils/format/price.go index 23603452..af736079 100644 --- a/utils/format/price.go +++ b/utils/format/price.go @@ -15,6 +15,6 @@ func CentitoDollar(price int64, remainFloatPoint ...uint) string { // 厘转美元 func CentitoDollarStr(price float64) string { - s := "%.2f" + s := "%0.2f" return fmt.Sprintf(s, price/float64(1000)) }