Reputation: 9024
I'm trying to use money_format
function.
<?php
$number = 1299.46;
setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $number); // Outputs 1299.46
While it should print $ sign or USD ?
I'm on linux hosting.
Thanks
Upvotes: 12
Views: 8772
Reputation: 41310
Locales are constants defined by the operation system.
I use Ubuntu
for the following example.
check which locales are supported:
locale -a
add the locales you want (for example ru
):
sudo locale-gen ru_RU
sudo locale-gen ru_RU.UTF-8
run this update comand
sudo update-locale
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
Upvotes: 5
Reputation: 51
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
Upvotes: 1
Reputation: 1908
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'.
Upvotes: 15
Reputation: 395
It shouldn't give $ or USD back, consult the documentation. However you can do the following:
echo money_format('$%i', $number)
This will give the expected result but ofcourse it is not a dynamic solution.
Upvotes: 0