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)...