0

When trying to remove tax (MySQL 5.7 database result DECIMAL(10,2) expressed as a strong via CakePHP on PHP 7.2.33) I perform the following:

$shipping_total = $order['Order']['shipping_cost']; 
$shipping_tax = $shipping_total/11; 
$shipping_price = $shipping_total - $shipping_tax; 

where $order['Order']['shipping_cost'] = '12.00' in the above, then add the results to an array:

$data['Lines'][] = [
    'Quantity' => 1,
    'Price' => round($shipping_price, 2), 
    'Tax' => round($shipping_tax, 2),
    'Total' => round($shipping_total, 2)
];

However when outputting the array (without any further manipulation to those lines) using json_encode it outputs the following:

"Lines": [
    {
        "Quantity": 1,
        "Price": 10.910000000000000142108547152020037174224853515625,
        "Tax": 1.0900000000000000799360577730112709105014801025390625,
        "Total": 12
    },
      

...which is causing the API to reject the input because, well, just look at it!

I understand there's weirdness around floats but I'm trying to find a workable solution to not make this happen so I can get on with this API integration. I can see that the number must still be stored as a float(), and the long decimals is probably triggered by the /11, which is causing the strange output but still can't find a way to get the output I want (without MAYBE converting them to a string and back, which is redonk)...

hellodaniel
  • 154
  • 7
  • I don't think its `json_encode()` that breaks it, that just converts whatever is in the array to a JSON string. What is `var_dump($data['Lines']);` in PHP? – Qirel Aug 19 '20 at 06:05
  • I've just tried `json_encode()` and get `{"Lines":[{"Quantity":1,"Price":10.91,"Tax":1.09,"Total":12}]}` as the output. – Nigel Ren Aug 19 '20 at 06:06
  • Demo works -- https://3v4l.org/0o0nh – Qirel Aug 19 '20 at 06:06
  • Thanks @Qirel - I cannot reproduce this anywhere other than the environment it's currently in - and you're right, it checks out. I suspect it's something to do with json_encode – hellodaniel Aug 19 '20 at 06:22
  • Try using `var_export($data)` as this may give more information on the data. – Nigel Ren Aug 19 '20 at 06:38
  • Best guess is this is similar to this: https://stackoverflow.com/questions/43865885/how-to-json-encode-float-values-in-php-7-1-1 and is an issue (?) with `json_encode` - what a nightmare though. I've gotten around it (for the time being) converting the values to strings – hellodaniel Aug 19 '20 at 06:38
  • Ah, you're right @NigelRen, it's not json_encode - var_export gives me: `, 'Price' => 10.910000000000000142108547152020037174224853515625, 'Tax' => 1.0900000000000000799360577730112709105014801025390625, 'Total' => 12.0,` sooo, still don't know – hellodaniel Aug 19 '20 at 06:45
  • Check each item when you add it, find any times that `$data` is set and make sure it's using round. If still not sure add an extra value to the array for each item (if possible) to see which statement generates the incorrect array lines. – Nigel Ren Aug 19 '20 at 06:47

0 Answers0