Reputation: 3375
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 echoes whole number...
Upvotes: 0
Views: 103
Reputation: 11393
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.
If you want to get 8 decimals in the number you display, the rounding must be performed after all the computations. You could modify the getUSDRate()
function to include the rounding there:
public static function getUSDRate($value)
{
$urldata = get_curl_content('https://example.com/');
$rates = json_decode($urldata, TRUE);
if (!$rates) {
return '-';
}
$usdRate = $rates['USD']['sell'];
if (!$usdRate) {
return '-';
}
return round($value/$usdRate, 8);
}
echo getUSDRate($singles->price);
Note: the declaration of getUSDRate
function indicates it is part of a class. In that case the call to this static function should be:
echo YOUR_CLASS_HERE::getUSDRate($singles->price);
Upvotes: 3