Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print Currency Number Format in PHP

I have some price values to display in my page.

I am writing a function which takes the float price and returns the formatted currency val with currency code too..

For example, fnPrice(1001.01) should print $ 1,000.01

like image 219
KoolKabin Avatar asked Oct 25 '10 11:10

KoolKabin


People also ask

What is number format PHP?

The number_format() function is an inbuilt function in PHP which is used to format a number with grouped thousands. It returns the formatted number on success otherwise it gives E_WARNING on failure. Syntax: string number_format ( $number, $decimals, $decimalpoint, $sep )

What is money format?

United States (U.S.) currency is formatted with a decimal point (.) as a separator between the dollars and cents. Some countries use a comma (,) instead of a decimal to indicate that separation.


1 Answers

The easiest answer is number_format().

echo "$ ".number_format($value, 2); 

If you want your application to be able to work with multiple currencies and locale-aware formatting (1.000,00 for some of us Europeans for example), it becomes a bit more complex.

There is money_format() but it doesn't work on Windows and relies on setlocale(), which is rubbish in my opinion, because it requires the installation of (arbitrarily named) locale packages on server side.

If you want to seriously internationalize your application, consider using a full-blown internationalization library like Zend Framework's Zend_Locale and Zend_Currency.

like image 82
Pekka Avatar answered Sep 28 '22 21:09

Pekka