Función PHP number_format()
❮ Referencia de cadenas de PHP
Ejemplo
Números de formato:
<?php
echo number_format("1000000")."<br>";
echo number_format("1000000",2)."<br>";
echo number_format("1000000",2,",",".");
?>
Definición y uso
La función number_format() formatea un número con miles agrupados.
Nota: Esta función admite uno, dos o cuatro parámetros (no tres).
Sintaxis
number_format(number,decimals,decimalpoint,separator)
Valores paramétricos
Parameter | Description |
---|---|
number | Required. The number to be formatted. If no other parameters are set, the number will be formatted without decimals and with comma (,) as the thousands separator. |
decimals | Optional. Specifies how many decimals. If this parameter is set, the number will be formatted with a dot (.) as decimal point |
decimalpoint | Optional. Specifies what string to use for decimal point |
separator | Optional. Specifies what string to use for thousands separator. Only the first character of separator is used. For example, "xxx" will give the same output as "x" Note: If this parameter is given, all other parameters are required as well |
Detalles técnicos
Valor devuelto: | Devuelve el número formateado |
---|---|
Versión PHP: | 4+ |
Registro de cambios: | A partir de PHP 5.4, esta función admite varios bytes en los parámetros decimalpoint y separator . En versiones anteriores, solo se usaba el primer byte de cada separador. |
Más ejemplos
Ejemplo
Desea devolver un precio: un parámetro redondeará el número (se formateará sin decimales). Dos parámetros deberían dar el resultado que desea:
<?php
$num = 1999.9;
$formattedNum = number_format($num)."<br>";
echo $formattedNum;
$formattedNum = number_format($num, 2);
echo $formattedNum;
?>
❮ Referencia de cadenas de PHP