1

PHP 7.3.15 LARAVEL 5.8.38

dump(ini_get('serialize_precision'));
// Out: "-1"

$data = DB::table('SOME QUERY HERE')->get();
dump($data);
 #items: array:10 [
    0 => {#713
      +"creation": "2020-04-23 04:00:00"
      +"user_name": "Some Client"
      +"amount": 17.8
      +"count": 3
    }

dump(json_encode($data, JSON_PRETTY_PRINT));
[
    {
        "creation": "2020-04-23 04:00:00",
        "user_name": "Some Client",
        "amount": 17.799999999999997 // Why?
        "count": 3
    }

What am I doing wrong here? I need the same precision here when I send the data in JSON to my Javascript client.

Thanks in advance.

UPDATE I was already doing this:

ini_set("serialize_precision", -1);

But this works:

dump(json_encode([1002.31, 2002.42]));
// Out: "[1002.31,2002.42]"

Seems the problem is when serializing Laravel collections?

Conde
  • 785
  • 15
  • 31

0 Answers0