PHP money_format() function

Example

en_US International Format:

<?php
$number = 1234.56;
setlocale(LC_MONETARY,"en_US");
echo money_format("The price is %i", $number);
?>

Salida del código anterior:

The price is USD 1,234.56

Definition and Usage

The money_format() function returns a string formatted as a currency string.

This function inserts a formatted number at the percentage sign (%) position in the main string.

Comentarios:The money_format() function does not work on the Windows platform.

Tip:This function is often used with setlocale() Functions are used together.

Tip:To view all available language codes, please visit ourLanguage Code Reference Manual.

Syntax

money_format(string,number)
Parameter Description
string

Required. Define the string to be formatted and how to format the variables within it.

Possible format values:

Padding and flags:

  • =f - Define the character (f) used as padding (for example: %=t uses "t" as padding). The default padding is spaces.
  • ^ - Remove the use of grouping characters.
  • + or ( - Define how positive and negative numbers are displayed. If using "+", the local settings of + and - are used (usually a sign is added before negative numbers, and no sign is added before gifts). If using "(", negative numbers are enclosed in parentheses. The default is to use "+".
  • ! - Stop using the currency symbol in the output string.
  • - If using "-", all fields are left-aligned. The default is right-aligned.

Field width:

  • x - Define the minimum width of the field (x). The default is 0.
  • #x - Define the maximum number of digits on the left side of the decimal point (x). Used to keep formatted output aligned in the same column. If the number of digits is greater than x, this rule will be ignored.
  • .x - Define el número máximo de dígitos a la derecha del punto decimal (x). Si x es 0, el punto decimal y los dígitos a su derecha no se mostrarán. Se utiliza la configuración local por defecto.

Caracteres de conversión:

  • i - El número se formatea en formato de moneda internacional.
  • n - El número se formatea en formato de moneda del país.
  • % - Devuelve el carácter %.

Comentarios:Si se utilizan varios valores de formato, deben aparecer en el orden mencionado anteriormente.

Comentarios:Esta función está influenciada por la configuración local.

number Obligatorio. El número insertado en la posición del símbolo % de la cadena de formato.

Detalles técnicos

Valor de retorno:

Se devuelve la cadena formateada.

Los caracteres antes y después de la cadena de formato se mantendrán inmutables y se devolverán. Los números no numéricos devolverán NULL y generarán E_WARNING.

Versión de PHP: 4.3.0+

Más ejemplos

Ejemplo 1

Formato internacional con 2 decimales (Alemania):

<?php
$number = 1234.56;
setlocale(LC_MONETARY,"de_DE");
echo money_format("%.2n", $number);
?>

Salida del código anterior:

1 234,56 EUR

Ejemplo 2

Número negativo, con () para indicar el número negativo en el formato internacional de EE. UU., Precisión a la derecha de 2 dígitos y "*" como carácter de relleno:

<?php
$number = -1234.5672;
echo money_format("%=*(#10.2n",$number);
?>

Salida del código anterior:

(******1234.57)