phpnew Posted August 12, 2020 Share Posted August 12, 2020 Hi, I have this money related function: function getMoneyUS( $pricesUS ) { setlocale( LC_MONETARY, 'en_US.UTF-8' ); return money_format( '%n', $pricesUS ); } Now, after the update to 7.4, it is showing in the logs as deprecated. The issue is money_format. I tried with NumberFormatter::formatCurrency but I either get an error or empty results. Examples I'm finding are not functions that can be applied multiple times, but for fixed values only. Examples like this: $fmt = numfmt_create( 'de_DE', NumberFormatter::CURRENCY ); echo numfmt_format_currency($fmt, 1234567.891234567890000, "EUR")."\n"; Or this: $fmt = new NumberFormatter( 'de_DE', NumberFormatter::CURRENCY ); echo $fmt->formatCurrency(1234567.891234567890000, "EUR")."\n"; I could not adapt them to my function. I understand that I do not need setlocale anymore, but this NumberFormatter only. But the exact working replacement is a challenge for me. I tried removing variable $fmt and replacing the fixed amount with my $priceUS, always some error or no result. Quote Link to comment https://forums.phpfreaks.com/topic/311322-function-money_format-is-deprecated/ Share on other sites More sharing options...
gw1500se Posted August 12, 2020 Share Posted August 12, 2020 This is what you need. Quote Link to comment https://forums.phpfreaks.com/topic/311322-function-money_format-is-deprecated/#findComment-1580576 Share on other sites More sharing options...
phpnew Posted August 12, 2020 Author Share Posted August 12, 2020 Official PHP website in most cases is not of much use for people that ask for help in places like here, on the forum. I was certainly first there, looking into four or five examples from 5 to 9 years ago that were just repeating the official ones. I could not use it for what I came up with at the end. Anyhow, I was able to figure it out, test and get no errors reported while getting the desired result. And here is the full code, for whoever needs it in the future: function getMoneyUS( $pricesUS ) { $formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY); return $formatter->formatCurrency($pricesUS, 'USD') . PHP_EOL; } function getMoneyUSd( $pricesUS ) { $formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY); $formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 0); return $formatter->formatCurrency($pricesUS, 'USD') . PHP_EOL; } It's for USD in this example. To change the currency you change the locale in the second line, then the currency code in the following one. The first function is for regular prices with decimals, and the second one is for amounts with no decimals. I need this to show the discount amounts that are always shown as rounded numbers, like Save $20 and so on. MAX_FRACTION_DIGITS sets the number of decimals. Thanks 1 Quote Link to comment https://forums.phpfreaks.com/topic/311322-function-money_format-is-deprecated/#findComment-1580577 Share on other sites More sharing options...
benanamen Posted August 12, 2020 Share Posted August 12, 2020 35 minutes ago, phpnew said: To change the currency you change the locale in the second line If you passed it in as a parameter you would never have to edit anything and could use the function for any currency Quote Link to comment https://forums.phpfreaks.com/topic/311322-function-money_format-is-deprecated/#findComment-1580578 Share on other sites More sharing options...
phpnew Posted August 12, 2020 Author Share Posted August 12, 2020 1 hour ago, benanamen said: If you passed it in as a parameter you would never have to edit anything and could use the function for any currency Eh! If I knew how to do that, I would not come here to ask for help. I change this now, and I'm good for another 3 years, lol. I don't do programming. 1 Quote Link to comment https://forums.phpfreaks.com/topic/311322-function-money_format-is-deprecated/#findComment-1580579 Share on other sites More sharing options...
requinix Posted August 12, 2020 Share Posted August 12, 2020 27 minutes ago, phpnew said: Eh! If I knew how to do that, I would not come here to ask for help. I change this now, and I'm good for another 3 years, lol. I don't do programming. Can we assume that within those next three years you'll learn how to "do programming"? Quote Link to comment https://forums.phpfreaks.com/topic/311322-function-money_format-is-deprecated/#findComment-1580580 Share on other sites More sharing options...
requinix Posted August 12, 2020 Share Posted August 12, 2020 4 hours ago, phpnew said: I tried with NumberFormatter::formatCurrency but I either get an error or empty results. Examples I'm finding are not functions that can be applied multiple times, but for fixed values only. Examples like this: $fmt = numfmt_create( 'de_DE', NumberFormatter::CURRENCY ); echo numfmt_format_currency($fmt, 1234567.891234567890000, "EUR")."\n"; Or this: $fmt = new NumberFormatter( 'de_DE', NumberFormatter::CURRENCY ); echo $fmt->formatCurrency(1234567.891234567890000, "EUR")."\n"; I could not adapt them to my function. I understand that I do not need setlocale anymore, but this NumberFormatter only. But the exact working replacement is a challenge for me. I tried removing variable $fmt and replacing the fixed amount with my $priceUS, always some error or no result. 2 hours ago, phpnew said: Anyhow, I was able to figure it out, test and get no errors reported while getting the desired result. And here is the full code, for whoever needs it in the future: function getMoneyUS( $pricesUS ) { $formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY); return $formatter->formatCurrency($pricesUS, 'USD') . PHP_EOL; } function getMoneyUSd( $pricesUS ) { $formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY); $formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 0); return $formatter->formatCurrency($pricesUS, 'USD') . PHP_EOL; } I don't see what the difference between the second example and your working version is - besides changing the locale string and currency ID. You had something that didn't work - what did you change so that it did work? Quote Link to comment https://forums.phpfreaks.com/topic/311322-function-money_format-is-deprecated/#findComment-1580581 Share on other sites More sharing options...
phpnew Posted August 13, 2020 Author Share Posted August 13, 2020 6 hours ago, requinix said: I don't see what the difference between the second example and your working version is - besides changing the locale string and currency ID. You had something that didn't work - what did you change so that it did work? Initially, I tried using it without "$formatter" variable. I thought that in functions I would use it as in the one with money_format, no $ sign except for the variable that is there for the price. That is why it did not work. I searched around, tried various things. Now as I'm typing this, i figure I obviously do not need that EOL which I guess stands for "End of line" so I'll take it out. Quote Link to comment https://forums.phpfreaks.com/topic/311322-function-money_format-is-deprecated/#findComment-1580582 Share on other sites More sharing options...
requinix Posted August 13, 2020 Share Posted August 13, 2020 2 hours ago, phpnew said: Initially, I tried using it without "$formatter" variable. I thought that in functions I would use it as in the one with money_format, no $ sign except for the variable that is there for the price. That is why it did not work. I searched around, tried various things. Now as I'm typing this, i figure I obviously do not need that EOL which I guess stands for "End of line" so I'll take it out. money_format() was a very simple thing. One of the reasons why it was deprecated. The Intl stuff is quite a bit more sophisticated, and being a different thing it also works differently. Most of them work by you setting up a "formatter" with whatever settings, then using that formatter to get what you want. Hypothetically you could set up that formatter at the beginning of your script and settings from your database or the user and then share it around so that all your code could use it - would save you from having to set it up over and over again. Quote Link to comment https://forums.phpfreaks.com/topic/311322-function-money_format-is-deprecated/#findComment-1580584 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.