Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to format a decimal number from a json object in PHP?

I’m trying to format a decimal number that I’ve optained from a json object in PHP. My objective is to keep this number with just two places after the comma.

Here is the code:

`

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$url = "https://api.yadio.io/exrates/EUR";
$data =  json_decode(file_get_contents($url, true));
$object = $data;
foreach($object as $a){
echo round($a->CUP, 2);
}

`

The result without round() was 163.905765, so when I apply it, the result is 0163.9100 and should be 163.90.

Any help will be very appretiate.

>Solution :

Your json response is of type –

{
"BTC": 20849.71,
"EUR": {
"CAD": 1.353201,
"CDF": 2030.444751,
"CHF": 0.990328,
"CLP": 939.150233,
"CNY": 7.204403,
"COP": 4731.844098,
"CRC": 617.225833,
"CUP": 163.905765,
},
"base": "EUR",
"timestamp": 1666977603763
}

So you are not looping around it correctly.

try this –

$url = "https://api.yadio.io/exrates/EUR";
$data =  json_decode(file_get_contents($url, true));
$object = $data;
echo round($object->EUR->CUP,2);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading