0

In Laravel I am trying to get data from 3rd party api but something is wrong with data.

In google chrome preview section data is correct but in response data is wrong and in Mysql DB also saving wrong data.

In Preview section :

{
  "buyAmount": 7.31,
  "clientRate": 1.826440,
  "marketRate": 1.835619,
} 

In response Section :

{
  "buyAmount": 7.30999999999999960920149533194489777088165283203125,
  "clientRate": 1.82644099999999998118482835707254707813262939453125,
  "marketRate": 1.835618999999999889638502281741239130496978759765625,
} 

And In DB In JSON formate :

{
  "buyAmount": 7.30999999999999960920149533194489777088165283203125,
  "clientRate": 1.82644099999999998118482835707254707813262939453125,
  "marketRate": 1.835618999999999889638502281741239130496978759765625,
} 

I tried with round((float) ($quotedJsonString["clientRate"]), 6) But nothing worked.

Here is the code :

$url = $this->config['baseUrl'];
$response = Http::get($url);

$res = $response->json();


$response = [
    "buyAmount" => round((float) ($res["buyAmount"]), 6),
    "clientRate" => round((float) ($res["clientRate"]), 6),
    "marketRate" => round((float) ($res["marketRate"]), 6),
];
$encoded = json_encode($response);
$quote = DBModel::create([
    "response" => $encoded,
]);

In postman and other API testing tool data is correct like Chrome section Even print_r() gives me right data.

I tried with abs() function but nothing worked

{
"buyAmount" => abs(($quotedJsonString["buyAmount"])),
            "clientRate" => abs(($quotedJsonString["clientRate"])),
            "marketRate" => abs(($quotedJsonString["marketRate"])),
}

Please Help I don't know is this laravel issue or php or something else.

Aks
  • 102
  • 8
  • Try with ```number_format``` function – Telexx Mar 31 '23 at 18:05
  • Tried but not worked – Aks Apr 01 '23 at 08:27
  • Hope this will solve your problem $response = [ "buyAmount" => number_format((float)$res["buyAmount"], 6, '.', '') "clientRate" => number_format((float)$res["clientRate"], 6, '.', ''), "marketRate" => number_format((float)$res["marketRate"], 6, '.', ''), ]; – Dip Ghosh Apr 01 '23 at 09:07
  • Thanks, But I solved this by adding ini_set('precision', 10); ini_set('serialize_precision', 10); These values – Aks Apr 01 '23 at 09:18

0 Answers0