paulmagm Posted November 5, 2021 Share Posted November 5, 2021 (edited) 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 November 5, 2021 by paulmagm Quote Link to comment https://forums.phpfreaks.com/topic/314169-format-numbers-with-a-helper-function/ Share on other sites More sharing options...
gw1500se Posted November 5, 2021 Share Posted November 5, 2021 More information. Does not work doesn't help. What error message do you get or what do you get that is different from what you expect? Do you have error reporting turned on? error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/314169-format-numbers-with-a-helper-function/#findComment-1591784 Share on other sites More sharing options...
paulmagm Posted November 5, 2021 Author Share Posted November 5, 2021 Page is empty from this point.. but no error. Also with error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/314169-format-numbers-with-a-helper-function/#findComment-1591785 Share on other sites More sharing options...
gizmola Posted November 5, 2021 Share Posted November 5, 2021 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, '.', '.'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/314169-format-numbers-with-a-helper-function/#findComment-1591786 Share on other sites More sharing options...
paulmagm Posted November 5, 2021 Author Share Posted November 5, 2021 What would be the best way to implement a numbers helper in my CMS? I have to show prices sometimes $ 45.555,64549 and $ 0,00067 and supply like 113.122.122,4567. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/314169-format-numbers-with-a-helper-function/#findComment-1591800 Share on other sites More sharing options...
Barand Posted November 5, 2021 Share Posted November 5, 2021 Can't you just let the users set their own system locale settings so they appear as they want. So in UK I would see 123.456 whereas someone in say, Germany, would see 123,456 Quote Link to comment https://forums.phpfreaks.com/topic/314169-format-numbers-with-a-helper-function/#findComment-1591802 Share on other sites More sharing options...
paulmagm Posted November 5, 2021 Author Share Posted November 5, 2021 (edited) Yes this is no problem but after the , how can i handle this that big prices set a 0 with number_format. But then my small prices like 0,00004 only looks like 0. i only have $price. Edited November 5, 2021 by paulmagm Quote Link to comment https://forums.phpfreaks.com/topic/314169-format-numbers-with-a-helper-function/#findComment-1591803 Share on other sites More sharing options...
gizmola Posted November 5, 2021 Share Posted November 5, 2021 Localization is a non-trivial issue. I'd suggest reading through this page. In particular you are interested in using setlocale appropriately for the locale/language/date/number/currency format you want to display. At that point a function like number_format should work for you. Quote Link to comment https://forums.phpfreaks.com/topic/314169-format-numbers-with-a-helper-function/#findComment-1591804 Share on other sites More sharing options...
paulmagm Posted November 5, 2021 Author Share Posted November 5, 2021 Yes thank you. Do you have a tipp for the problem that i dont want $ 65.400,678956 but i need to display $ 0,000134. With one value like $price. Quote Link to comment https://forums.phpfreaks.com/topic/314169-format-numbers-with-a-helper-function/#findComment-1591805 Share on other sites More sharing options...
paulmagm Posted November 6, 2021 Author Share Posted November 6, 2021 Found a solution with the function on top. Quote Link to comment https://forums.phpfreaks.com/topic/314169-format-numbers-with-a-helper-function/#findComment-1591811 Share on other sites More sharing options...
gizmola Posted November 6, 2021 Share Posted November 6, 2021 I don't think you should ever have a different decimal point format for a feature like this, as you typically right align numbers so that they can be easily compared. Quote Link to comment https://forums.phpfreaks.com/topic/314169-format-numbers-with-a-helper-function/#findComment-1591826 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.