Last updated on August 8, 2015
You often have to display money numbers in business applications like eCommerce apps. So in general we fetch the value from the database and print it to the webpage with symbol concatenation as shown below code.
If you want to seriously internationalize your application, consider using a full-blown internationalization library like Zend Framework’s Zend_Locale
and Zend_Currency
.
$amount = 1235252.000; echo ” The Amount is $”.$amount;
PHP has money_format library function which can convert the number in to properly formatted with currency symbol.
Look at below code.
".money_format("The price US dollar is %n", $number); setlocale(LC_MONETARY, 'en_GB.UTF-8'); echo "
". money_format("The price UK pound is %n", 220220.25); // £1235252.16 setlocale(LC_MONETARY, 'en_IN.UTF-8'); echo "
".money_format('The price Indian rupee is %n', 250250.75); // ₹ 1235252.16 ?>
Output of the above code is as mentioned below.
The price US dollar is $3,030.25
The price UK pound is £220,220.25
The price Indian rupee is ₹ 2,50,250.75
You can see how money_format nicely set currency symbol and format the number with comma.
Important notice before money_format
you have to call setlocale
method and set ISO code for currency. money_format()
doesn’t work on Windows and relies on setlocale()
and it requires the installation of (arbitrarily named) locale packages on server side.
TIP : if you want to format only number you can use number_format()
function something like this “echo "$ ".number_format($value, 2);