Jump to content

format numbers with a helper / function


paulmagm
 Share

Recommended Posts

function price_format($price, $decimals = 2) {
    $CI =& get_instance();
    $config = $CI->config->item('mcp');

    $price = (float) $price;
    $formats = ["US" => ['.', ','], "EU" => [',', '.']];
    $decimals = ($price >= 1 || $price == 0) ? $decimals : ($price < 0.000001 ? 8 : 6);
    return number_format($price, $decimals, $formats[$config['num_format']][0], $formats[$config['num_format']][1]);

$coinfeed_coingecko_json = file_get_contents('https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids='.$t['page.content_slug'].'');
$coinfeed_json = json_decode($coinfeed_coingecko_json, false);
$coinfeed_current_price = $coinfeed_json[0]->current_price;
$coinfeed_current_supply = $coinfeed_json[0]->circulating_supply;
$coinfeedprice_change_percentage_24h = $coinfeed_json[0]->price_change_percentage_24h;
$coinfeed_ath = $coinfeed_json[0]->ath;

?>

<?php  echo $coinfeedde = number_format($coinfeed_current_price, 0, '.', '.');  ?>

 

I created my first api call and now i trying to connect it to my old numbers helper (function price_format).
How can i connect this function with my new api call? If i replace $price it will not work. Thanks for your time guys.

 

Edited by paulmagm
Link to comment
Share on other sites

Can we start with the fact that this is not a valid function:

function price_format($price, $decimals = 2) {
    $CI =& get_instance();
    $config = $CI->config->item('mcp');

    $price = (float) $price;
    $formats = ["US" => ['.', ','], "EU" => [',', '.']];
    $decimals = ($price >= 1 || $price == 0) ? $decimals : ($price < 0.000001 ? 8 : 6);
    return number_format($price, $decimals, $formats[$config['num_format']][0], $formats[$config['num_format']][1]);

You're missing the closing curly bracket!

You can lint your php files using command line php with php -l your_file_name.php, and it will show you if the file contains valid php or errors.  

Then you show more code, but you are never using price_format().  Instead you have:

<?php  echo $coinfeedde = number_format($coinfeed_current_price, 0, '.', '.');  ?>
Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.