Csongor Halmai
Csongor Halmai

Reputation: 3885

Why money_format() PHP function does not format the numbers?

I want to format numbers according to the desired locale setting. So, I am using the example code from the PHP manual:

$number = 1234.56;

// let's print the international format for the en_US locale
setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $number) . "\n";
// USD 1,234.56

Instead of the expected result, I just get the unformatted value "1234.56". The same happens if I use a different locale, for example 'it_IT' instead of 'en_US'.

Why doesn't if format?

Upvotes: 0

Views: 581

Answers (2)

Csongor Halmai
Csongor Halmai

Reputation: 3885

The PHP manual tells that the money_format( string $format , float $number ) function relies on the strfmon() C-function.

The possible options you can pass as the $format argument are the ones that the locale command of your operating system can understand. If you pass something different then the number is not formatted at all, it is just converted to string. This is what has happened to me.

In order to list the accepted values, you can use this command from the prompt:

locale -a 

Depending on your installation, it may list only a couple of locales. In order to use more, you may want to install other locales. In Debian you can do it this way:

apt-get install locales
apt-get install locales-all

After installing all the locales, the locale -a command lists hundreds of locales. Now the number_format() function works as you expected.

Upvotes: 0

LF-DevJourney
LF-DevJourney

Reputation: 28529

For there is no en_US locals in your envs. USe locale -a to check it.

You've to install it. Refer to this answer

Upvotes: 1

Related Questions