Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/311322-function-money_format-is-deprecated/
Share on other sites

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

  • Like 1
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.

  • Like 1
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"?

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?

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

×
×
  • 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.