Garg Garg - 1 year ago 33
PHP Question

Simple round function doesn't round

I'm trying to round one value to 8 symbol after decimal but it doesn't round anything. For example:

12/653.44
result: 0.018364348677767


I want to round and output 8 symbols only after
,

This is the function

public static function getUSDRate()
{
$urldata = get_curl_content('https://example.com/');
$rates = json_decode($urldata, TRUE);
if (!$rates) {
return '-';
}
$usdRate = $rates['USD']['sell'];
if (!$usdRate) {
return '-';
}
return round($usdRate, 8);
}


Function calling

$singles->price/getUSDRate()


Then when I call the function it echo whole number...

Answer Source

If you want the result to have only 8 decimals, you should use:

echo round($singles->price/getUSDRate(), 8);

With the information in your question, we can see that you are rounding too early, since you perform more calculations later. You could probably remove the rounding from getUSDRate() function.