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..af736079 100644 --- a/utils/format/price.go +++ b/utils/format/price.go @@ -2,23 +2,19 @@ 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)) } // 厘转美元 func CentitoDollarStr(price float64) string { - s := "%.2f" + s := "%0.2f" return fmt.Sprintf(s, price/float64(1000)) }