php - Simple round function doesn't round -
i'm trying round 1 value 8 symbol after decimal doesn't round anything. example:
12/653.44 result: 0.018364348677767 i want round , output 8 symbols after ,. 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() when call function echoes whole number...
if want result have 8 decimals, should use:
echo round($singles->price/getusdrate(), 8); with information in question, can see rounding early, since perform more calculations later. remove rounding getusdrate() function.
if want 8 decimals in number display, rounding must performed after computations. modify getusdrate() function include 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: declaration of getusdrate function indicates part of class. in case call static function should be:
echo your_class_here::getusdrate($singles->price);
Comments
Post a Comment