0

I do a calculation in PHP which gives me decimal values. So far no problem. When I put these values in an array to make them client-side, some values are very badly formatted.

$res = [
    "amount" => $this->amount_cfa,
    "x_fee" => $this->x_fee,
    "part_fee" => $this->part_fee,
    "total" => $total,
    "dest" => $this->dest
];

Log::info($res);

return response($res);

The total element is the one that poses the problem. if the total is 7200.6, on the client side I get 7200.599999999999. When I log the array

[2021-07-15 04:08:28] local.INFO: array (
  'amount' => '7200.00',
  'x_fee' => 0.36,
  'part_fee' => 0.24,
  'total' => 7200.599999999999,
  'dest' => '67349555',
)  

However when I display the total before being added to the table it displays 7200.6

Where does this problem come from?

Thank you in advance for your help

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43

1 Answers1

0
$res = [
    "amount" => $this->amount_cfa,
    "x_fee" => $this->x_fee,
    "part_fee" => $this->part_fee,
    "total" => round($total, 2),
    "dest" => $this->dest
];
Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97