Parcourir la source

优化:浮点数计算精度问题

萤火科技 il y a 4 ans
Parent
commit
5e4e02bc30

+ 4 - 3
app/api/service/order/Checkout.php

@@ -440,7 +440,7 @@ class Checkout extends BaseService
         // 设置商品积分赠送数量
         foreach ($this->goodsList as &$goods) {
             // 积分赠送比例
-            $ratio = $setting['gift_ratio'] / 100;
+            $ratio = helper::bcdiv($setting['gift_ratio'], 100);
             // 计算抵扣积分数量
             $goods['points_bonus'] = !$goods['is_points_gift'] ? 0 : helper::bcmul($goods['total_pay_price'], $ratio, 0);
         }
@@ -557,12 +557,13 @@ class Checkout extends BaseService
         foreach ($this->goodsList as &$goods) {
             $goodsKey = "{$goods['goods_id']}-{$goods['goods_sku_id']}";
             if (isset($rangeGoodsList[$goodsKey])) {
-                $goods['coupon_money'] = $rangeGoodsList[$goodsKey]['coupon_money'] / 100;
+                $goods['coupon_money'] = helper::bcdiv($rangeGoodsList[$goodsKey]['coupon_money'], 100);
             }
         }
+
         // 记录订单优惠券信息
         $this->orderData['couponId'] = $couponId;
-        $this->orderData['couponMoney'] = helper::number2($CouponMoney->getActualReducedMoney() / 100);
+        $this->orderData['couponMoney'] = helper::number2(helper::bcdiv($CouponMoney->getActualReducedMoney(), 100));
     }
 
     /**

+ 2 - 2
app/common/library/helper.php

@@ -114,9 +114,9 @@ class helper
     {
         $sum = 0;
         foreach ($array as $item) {
-            $sum += $item[$column] * 100;
+            $sum = self::bcadd($sum, $item[$column]);
         }
-        return $sum / 100;
+        return $sum;
     }
 
     /**

+ 3 - 3
app/common/library/wechat/WxPay.php

@@ -76,7 +76,7 @@ class WxPay extends WxBase
             'openid' => $openid,
             'out_trade_no' => $orderNo,
             'spbill_create_ip' => \request()->ip(),
-            'total_fee' => $totalFee * 100, // 价格:单位分
+            'total_fee' => helper::bcmul($totalFee, 100), // 价格:单位分
             'trade_type' => 'JSAPI',
         ];
         // 生成签名
@@ -174,8 +174,8 @@ class WxPay extends WxBase
             'nonce_str' => $nonceStr,
             'transaction_id' => $transactionId,
             'out_refund_no' => $time,
-            'total_fee' => $totalFee * 100,
-            'refund_fee' => $refundFee * 100,
+            'total_fee' => helper::bcmul($totalFee,100),
+            'refund_fee' => helper::bcmul($refundFee,100),
         ];
         // 生成签名
         $params['sign'] = $this->makeSign($params);