money_format — Formats a number as a currency string

We will use different locales and format specifications to illustrate the use of this function.

  1. <html>  
  2.  <body>  
  3. <?php
  4. $number = 1234.56;
  5. // let’s print the international format for the en_US locale
         style=”color: #0000bb;”>setlocale(LC_MONETARY, ‘en_US’);
  6. echo money_format(‘%i’, $number) . “\n”;
  7. // USD 1,234.56
  8. // Italian national format with 2 decimals`
  9. setlocale(LC_MONETARY, ‘it_IT’);
  10. echo money_format(‘%.2n’, $number) . “\n”;
  11. // L. 1.234,56
  12. // Using a negative number
  13. $number = –1234.5672;
  14. // US national format, using () for negative numbers
  15. // and 10 digits for left precision
  16. setlocale(LC_MONETARY, ‘en_US’);
  17. echo money_format(‘%(#10n’, $number) . “\n”;
  18. // ($ 1,234.57)
  19. // Similar format as above, adding the use of 2 digits of right
  20. // precision and ‘*’ as a fill character
  21. echo money_format(‘%=*(#10.2n’, $number) . “\n”;
  22. // ($********1,234.57)
  23. // Let’s justify to the left, with 14 positions of width, 8 digits of
  24. // left precision, 2 of right precision, withouth grouping character
  25. // and using the international format for the de_DE locale.
  26. setlocale(LC_MONETARY, ‘de_DE’);
  27. echo money_format(‘%=*^-14#8.2i’, 1234.56) . “\n”;
  28. // DEM 1234,56****
  29. // Let’s add some blurb before and after the conversion specification
  30. style=”color:#0000bb;”>setlocale(LC_MONETARY, ‘en_GB’);
  31. $fmt = ‘The final value is %i (after a 10%% discount)’;
  32. echo money_format($fmt, 1234.56) . “\n”;
  33. // The final value is GBP 1,234.56 (after a 10% discount)
  34. ?>
  35.  </body>   
  36. </html>  
eXTReMe Tracker