Very short snippet: formattare valute in PHP

mercoledì 11 febbraio, 2009

PHP mette a disposizione due comode funzioni per formattare ( e stampare ) valute: money_format() e number_format(). La prima può essere utilizzata insieme a setlocale() per impostare il tipo di formattazione in base al proprio paese, ad esempio:

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

Per noi europei, o italiani, possiamo usare:

PHP:
  1. $number = 1234.56;
  2. setlocale(LC_MONETARY, 'it_IT');
  3. echo money_format('%.2n', $number);
  4. // EUR 1.234,56

Tuttavia, nei casi più semplici e se vi da fastidio EUR, potete usare:

PHP:
  1. $number = 1234.56;
  2. echo number_format( $number, 2, ',', '.')
  3. // 1.234,56

Post correlati

Questo articolo ti è stato utile?: Per nientePocoAbbastanzaMoltoMoltissimo
Loading ... Loading ...

Un commento a: “Very short snippet: formattare valute in PHP”

  1. 11 feb, 2009 Luca:

    Cacchio non avevo mai visto money_format per php! grazie mille per i tip!

Lascia un commento

TAG XHTML PERMESSI: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERIMENTO CODICE:
<pre></pre>         // blocco generico
[code][/code]       // blocco generico
[as][/as]           // Actionscript
[css][/css]         // CSS Style Sheet
[html][/html]       // HTML
[js][/js]           // Javascript
[objc][/objc]       // Objective-C
[php][/php]         // PHP
[sql][/sql]         // SQL