Very short snippet: Currency format in PHP

. PHP offers two convenient functions to format (and print) Currency: money_format() and number_format() . The first can be used in conjunction with setlocale() to set the format type according to their own country, for example:

1
2
3
4
1234.56 ; $ Number = 1234.56;
LC_MONETARY , 'en_US' ) ; setlocale (LC_MONETARY, 'en_US');
( '%i' , $number ) ; echo money_format ('% s', $ number);
/ / 1,234.56 USD

For us Europeans, and Italians, we can use:

1
2
3
4
1234.56 ; $ Number = 1234.56;
LC_MONETARY , 'it_IT' ) ; setlocale (LC_MONETARY, 'en_US');
( '%.2n' , $number ) ; echo money_format ('% .2 n', $ number);
/ / EUR 1234.56

However, in the simplest cases, and if it bothers EUR , you can use:

1
2
3
1234.56 ; $ Number = 1234.56;
( $number , 2 , ',' , '.' ) echo number_format ($ number, 2, ',', '.')
/ / 1234.56

A comment: "Very short snippet: Currency format in PHP"

  1. February 11, 2009 Luke :

    Damn I had never seen money_format for php! thanks for the tip!

Leave a comment

TAG XHTML PERMITS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA