The money_format Function

The money_format function converts a number into a string formatted as a monetary value according to the current locale. It accepts a format string as the first parameter and the number to format as the second. The function is only available on systems that support strfmon (e.g., Linux).

Syntax

phpmoney_format(format, number);

Example

Formatting a number into a monetary format for the US locale:

php<?php
  setlocale(LC_MONETARY, 'en_US');
  echo money_format('%i', 1234.56);
?>

Code execution result:

php'USD 1,234.56'

Example

Formatting with precision and currency symbol specification:

php<?php
  setlocale(LC_MONETARY, 'de_DE');
  echo money_format('%.2n', 1234.56);
?>

Code execution result:

php'1.234,56 EUR'

Example

Formatting a negative number:

php<?php
  setlocale(LC_MONETARY, 'en_GB');
  echo money_format('%i', -1234.56);
?>

Code execution result:

php'-£1,234.56'

See Also

  • the number_format function,
    which formats a number with thousands separators
  • the sprintf function,
    which returns a formatted string