money_format() not showing currency

Locales are constants defined by the operation system.

I use Ubuntu for the following example.

  1. check which locales are supported:

    locale -a
    
  2. add the locales you want (for example ru):

    sudo locale-gen ru_RU
    sudo locale-gen ru_RU.UTF-8
    
  3. run this update comand

    sudo update-locale 
    
  4. restart php fpm ( check your php version service name like soservice --status-all)

    sudo service php7.2-fpm restart
    

But be careful, because most likely you are useing Apache with "mod_php" because it is the most popular one. And this one works in multi thread environment, when setlocale() is not thread safe. Look here for details


money_format will not work properly if locale is not valid.

For example, on Debian, 'en_US' is not a valid locale - you need 'en_US.UTF-8' or 'en_US.ISO-8559-1'.


First you must know php version is using

If you are unsure how to check create a file called info.php

Insert code => <?php phpinfo(); ?>

(PHP 4 >= 4.3.0, PHP 5)
$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

// Italian national format with 2 decimals`
setlocale(LC_MONETARY, 'it_IT');
echo money_format('%.2n', $number) . "\n";
// Eu 1.234,56

Note: The function money_format() is only defined if the system has strfmon capabilities. For example, Windows does not, so money_format() is undefined in Windows.

You can use an alternative (PHP 5 >= 5.3.0, PECL intl >= 1.0.0)

Example #1 numfmt_format_currency() example

$fmt = numfmt_create( 'de_DE', NumberFormatter::CURRENCY );
echo numfmt_format_currency($fmt, 1234567.891234567890000, "EUR")."\n";
echo numfmt_format_currency($fmt, 1234567.891234567890000, "RUR")."\n";
$fmt = numfmt_create( 'ru_RU', NumberFormatter::CURRENCY );
echo numfmt_format_currency($fmt, 1234567.891234567890000, "EUR")."\n";
echo numfmt_format_currency($fmt, 1234567.891234567890000, "RUR")."\n";

Example #2 OO example

$fmt = new NumberFormatter( 'de_DE', NumberFormatter::CURRENCY );
echo $fmt->formatCurrency(1234567.891234567890000, "EUR")."\n";
echo $fmt->formatCurrency(1234567.891234567890000, "RUR")."\n";
$fmt = new NumberFormatter( 'ru_RU', NumberFormatter::CURRENCY );
echo $fmt->formatCurrency(1234567.891234567890000, "EUR")."\n";
echo $fmt->formatCurrency(1234567.891234567890000, "RUR")."\n";

The above example will output:

1.234.567,89 €
1.234.567,89 RUR
1 234 567,89€
1 234 567,89р.

Examples taken from php.net

If the alternative is not possible, the solution is to create a method for your needs.

I hope helped you

Tags:

Php