添加两个数字时总是会出错

mk_*_*_89 0 php math

这是我到目前为止遇到的最奇怪的问题之一,我有两个数字,我试图在PHP中加起来但由于某种原因php没有给我正确的结果.

我尝试添加$itemPrice$shipPrice3.50 + 2.80应该给我6.30,而是我得到5.

我尝试过使用floatVal() function但没有区别,有没有人有任何想法?

下面是代码示例

PHP代码

foreach($resp->ListOrderItemsResult->OrderItems->OrderItem as $order){
   $itemPrice =  $order->ItemPrice->Amount;
   $shipPrice = $order->ShippingPrice->Amount;
   $total = $itemPrice + $shipPrice;

   $arr[] = array(
      'sku' => $order->SellerSKU,
      'isbn' => $order->ASIN,
      'title' => $order->Title,
      'item_price' => $itemPrice,
      'ship_price' => $shipPrice,
      'total' => $total,
      'quantity_shipped' => $order->QuantityShipped,
   );
}
Run Code Online (Sandbox Code Playgroud)

产量

sku             isbn         title  item_price  ship_price  total   quantity_shipped
VM-F5TU-BN0K    1844831531   xxxxx  3.50        2.80            5   1
Run Code Online (Sandbox Code Playgroud)

Mik*_*ant 5

我的猜测是$ itemPrice和$ shipPrice是字符串,当你尝试将它们加在一起时,你获得的值为5.如果你的对象中存储了字符串,请确保你专门将$ itemPrice和$ shipPrice转换为浮点数.