Home > front end >  How to print data in json that has 0 decimal point
How to print data in json that has 0 decimal point

Time:10-28

I have this Curl makeup

<?php
$url ="https://min-api.cryptocompare.com/data/price?fsym=USD&tsyms=BTC";


$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 120,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_HTTPHEADER => array(
        "Cache-Control: no-cache",
    ),
));

$response = curl_exec($curl);
$err = curl_error($curl);
$json = json_decode($response);
echo $json->BTC;
curl_close($curl);

Trying to get the value of BTC, I got 1.659E-5 instead of 0.00001659. Please I need help on how to decode this without this wrong figure.

CodePudding user response:

PHP automatically uses scientific notation for smaaall numbers display.

You can use (s)printf() to control formatting :

// 8 decimals
printf('%.8f', $json->BTC); // 0.00001654
  • Related