0

I have the following problem: when calculating the cost for delivery of an order, I noticed that in some cases (-1.4210854715202E-14 in my case) a floating point error shows up. You can find the function I am using below, the error shows up when $orderTotal has the same value as $totalPackagePrice.

public function getPaidPackages()
{
    $orderTotal = doubleval($this->total_price); //price the customer paid for
    $totalPackagePrice = doubleval($this->getTotalOrderPrice()); //price of all the orderitems within all the packages combined

    //error shows up when substracting the same values (e.g. 68.55 in my case)
    $packagingCosts = $orderTotal - $totalPackagePrice; //shows a negative value when debugging

    ...
}

Any help to tackle this problem is appreciated, so far I've tried to cast both input values to a float or a double to no avail.

J. Doe
  • 101
  • 10
  • It might be useful to see that numbers that are being used in this calculation when the issue occurs – RiggsFolly Aug 19 '17 at 16:54
  • `-1.4210854715202E-14` is basically `0`. Just because a quantity is mathematically `0`, it doesn't imply that your float calculations will result in `0` exactly. That isn't how floats work. – John Coleman Aug 19 '17 at 16:57
  • Try `money_format` or `number_format` on the result and you should be fine, eventually can also see the `bcsub`. – Maciej Kwas Aug 19 '17 at 17:01

0 Answers0