number_format FORMATS a number whit grouped thousands.
This function prior PHP 8.0.XX accepts ONE, TWO or FOUR parameters, never THREE.
This function started to accept THREE arguments in PHP 8.0.XX.
Although the FLOAT type is requested for $num in the function formulation, we have seen that the STRING and INTEGER types are also accepted.
Of course, the STRING type must have numeric connotation.
<?php
string number_format ( float $num,
int $decimals = 0,
?string $decimal_separator = ".",
?string $thousands_separator = "," )
where,
$num = The NUMBER to be formated
$decimals = To set the number of decimal digits
$decimal_separator = To set the separator for the decimal point
$thousands_separator = To set the thousands separator
?>
$num
The NUMBER to be formated.
$decimals
To set the number of decimal points.
If $decimals = 0, the $decimal_separator is omitted from the return value.
$decimal_separator
To set the separator for decimal points.
The default value is $decimal_separator = "," .
$thousands_separator
To set the thousands separator.
The default value is $thousands_separator = "." .
EXERCISE
<?php
/* You should note that the
* LOCALE indication is
* perfectly unnecessary
*/
// STRING
$nbr01s = '1234567';
// INTEGER
$nbr01n = 123456;
// AMERICAN LOCALE
setlocale(LC_ALL, "en_US", "en-US");
echo 'LIKE A LOCALE: "en_US"<br><br>';
echo 'STRING<br>';
$nbr01se = number_format($nbr01s, 8);
echo $nbr01se . '<br><br>';
echo 'FLOAT<br>';
$nbr01ne = number_format($nbr01n, 8);
echo $nbr01ne . '<br><br><br>';
// BRAZILIAN LOCALE
setlocale(LC_ALL, "pt_BR.utf-8", "pt-BR");
echo 'LIKE A LOCALE: "pt_BR"<br><br>';
echo 'STRING<br>';
$nbr01sb = number_format($nbr01s, 8);
echo $nbr01sb . '<br><br>';
echo 'FLOAT<br>';
$nbr01nb = number_format($nbr01n, 8);
echo $nbr01nb . '';
?>
EXERCISE
<?php
echo 'INPUT FORMATTING<br><br>';
$nbr01s = "123456789";
echo 'AS STRING ';
echo '=> "'. $nbr01s . '"<br><br><br>';
echo 'ZERO DECIMAL PLACES,<br>ONE PARAMETER:<br>';
$frnbr01s = number_format($nbr01s);
echo 'number_format("' . $nbr01s . '") <br><br>RESULTING: ' . $frnbr01s . '<br><br><br>';
echo 'TWO DECIMAL PLACES,<br>TWO PARAMETERS:<br>';
$frnbr02s = number_format($nbr01s , 2);
echo 'number_format("' . $nbr01s . '", ' . 2 . ') <br><br>RESULTING: ' . $frnbr02s . '<br><br><br>';
echo 'EIGHT DECIMAL PLACES,<br>TWO PARAMETERS:<br>';
$frnbr08s = number_format($nbr01s , 8);
echo 'number_format("' . $nbr01s . '", ' . 8 . ') <br><br>RESULTING: ' . $frnbr08s . '<br><br><br>';
ECHO 'NINE DECIMAL PLACES,<br>FOUR PARAMETERS EUROPEAN FORMAT 1:<br>';
$frnbr09s = number_format($nbr01s , 9, ',', '.');
echo 'number_format("' . $nbr01s . '", ' . 9 . ', \',\', \'.\') <br><br>RESULTING: ' . $frnbr09s . '<br><br><br>';
echo 'NINE DECIMAL PLACES,<br>FOUR PARAMETERS EUROPEAN FORMAT 2:<br>';
$frnbr09as = number_format($nbr01s , 9, ',', '');
echo 'number_format("' . $nbr01s . '", ' . 9 . ', \',\', \'\') <br><br>RESULTING: ' . $frnbr09as . '<br><br><br>';
?>
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CAUTION - SEVERAL ERRORS
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
echo 'INPUT FORMATTING<br><br><br>';
// STRING
$nbr011s = "345,6";
// The decimal separator is the point and not the comma
echo 'AS STRING => "';
echo $nbr011s . '"<br><br><br>';
// zero decimal places, one parameter
$frnbr011s = number_format($nbr011s);
echo 'number_format("' . $nbr011s . '") -> ' .
$frnbr011s . '<br><br><br>';
// two decimal places, two parameters
$frnbr021s = number_format($nbr011s , 2);
echo 'number_format("' . $nbr011s . '", ' . 2 . ') -> ' .
$frnbr021s . '<br><br><br>';
// eight decimal places, two parameters
$frnbr081s = number_format($nbr011s , 8);
echo 'number_format("' . $nbr011s . '", ' . 8 . ') -> ' .
$frnbr081s . '<br><br><br>';
// nine decimal places, four parameters European format 1
$frnbr091s = number_format($nbr011s , 9, ',', '.');
echo 'number_format("' .
$nbr011s . '", ' . 9 . ', \',\', \'.\') ->' . $frnbr091s . '<br><br><br>';
// nine decimal places, four parameters European format 2
$frnbr091as = number_format($nbr011s , 9, ',', '');
echo 'number_format("' .
$nbr011s . '", ' . 9 . ', \',\', \'\') ->' .
$frnbr091as . '<br><br><br>';
?>
EXERCISE
<?php
echo 'INPUT FORMATTING<br><br>';
$nbr04s = 123456789;
echo 'AS NUMBER ';
echo '=> "'. $nbr04s . '"<br><br><br>';
echo 'ZERO DECIMAL PLACES,<br>ONE PARAMETER:<br>';
$frnbr01s = number_format($nbr04s);
echo 'number_format("' . $nbr04s . '") <br><br>RESULTING: ' . $frnbr01s . '<br><br><br>';
echo 'TWO DECIMAL PLACES,<br>TWO PARAMETERS:<br>';
$frnbr02s = number_format($nbr04s , 2);
echo 'number_format("' . $nbr04s . '", ' . 2 . ') <br><br>RESULTING: ' . $frnbr02s . '<br><br><br>';
echo 'EIGHT DECIMAL PLACES,<br>TWO PARAMETERS:<br>';
$frnbr08s = number_format($nbr04s , 8);
echo 'number_format("' . $nbr04s . '", ' . 8 . ') <br><br>RESULTING: ' . $frnbr08s . '<br><br><br>';
ECHO 'NINE DECIMAL PLACES,<br>FOUR PARAMETERS EUROPEAN FORMAT 1:<br>';
$frnbr09s = number_format($nbr04s , 9, ',', '.');
echo 'number_format("' . $nbr04s . '", ' . 9 . ', \',\', \'.\') <br><br>RESULTING: ' . $frnbr09s . '<br><br><br>';
echo 'NINE DECIMAL PLACES,<br>FOUR PARAMETERS EUROPEAN FORMAT 2:<br>';
$frnbr09as = number_format($nbr04s , 9, ',', '');
echo 'number_format("' . $nbr04s . '", ' . 9 . ', \',\', \'\') <br><br>RESULTING: ' . $frnbr09as . '<br><br><br>';
?>
EXERCISE
<?php
$values =[ 1234.5678,
-1234.5678,
1234.6578e4,
-1234.56789e4,
0x1234CDEF,
02777777777,
"123456789",
"123.456789",
"12.3456789e1",
null,
true,
false ];
echo "DEFAULT:<br><br>";
for ($i = 0; $i < count($values); $i++) {
$res = number_format($values[$i]);
var_dump($res);
echo '<br><br>';
}
echo "<br><br>WITH TWO DECIMAL PLACES:<br><br>";
for ($i = 0; $i < count($values); $i++) {
$res = number_format($values[$i], 2);
var_dump($res);
echo '<br><br>';
}
echo "<br><br>ENGLISH FORMAT:<br><br>";
for ($i = 0; $i < count($values); $i++) {
$res = number_format($values[$i], 2, '.', ' ');
var_dump($res);
echo '<br><br>';
}
echo "<br><br>FRENCH FORMAT:<br><br>";
for ($i = 0; $i < count($values); $i++) {
$res = number_format($values[$i], 2, ',' , ' ');
var_dump($res);
echo '<br><br>';
}
?>
EXERCISE
<?php
$values = array(1234.5678,
-1234.5678,
1234.6578e4,
-1234.56789e4,
0x1234CDEF,
02777777777,
"123456789",
"123.456789",
"12.3456789e1",
null,
true,
false);
echo "MULTIPLE CHARACTER DECIMAL POINT:<br><br>";
for ($i = 0; $i < count($values); $i++) {
$res = number_format($values[$i], 2, '·', ' ');
var_dump($res);
echo '<br><br>';
}
echo "<br><br>MULTIPLE CHARACTER THOUSAND SEPARATOR:<br><br>";
for ($i = 0; $i < count($values); $i++) {
$res = number_format($values[$i], 2, '.' , ' ');
var_dump($res);
echo '<br><br>';
}
echo "<br><br>MULTIPLE CHARACTER DECIMAL AND THOUSEP:<br><br>";
for ($i = 0; $i < count($values); $i++) {
$res = number_format($values[$i], 2, '·' , ' ');
var_dump($res);
echo '<br><br>';
}
?>
EXERCISE
<?php
$number = -1.15E-15;
var_dump($number);
echo '<br><br>';
var_dump(number_format($number, 2));
echo '<br><br>';
var_dump(number_format(-0.01, 2));
?>
EXERCISE
<?php
echo 'PHP ' . PHP_VERSION . '<br><br>';
if (PHP_MAJOR_VERSION > 7)
{
$number = 2020.1415;
$arr = [ 0 => " D ", 1 => " 212121 ",
2 => 212121,
3 => null,
4 => true,
5 => " ",
6 => '.' ];
foreach($arr as $k => $v)
{
echo number_format($number, 10, $v) . '<br>';
}
}
else
{
echo '3 arguments is only possible as of PHP 8.0.XX';
}
?>
EXERCISE
<?php
$number = 2020.1415;
var_dump(number_format($number, 2, null, 'T'));
echo '<br><br>';
var_dump(number_format($number, 2, 'F', null));
?>
EXERCISE
<?php
echo "Testing number_format() : basic functionality.<br>";
$values = [ 1234.5678,
-1234.5678,
1234.6578e4,
-1234.56789e4,
0x1234CDEF,
02777777777,
"123456789",
"123.456789",
"12.3456789e1",
null,
true,
false ];
echo "<br><br>DEFAULT:<br><br>";
for ($i = 0; $i < count($values); $i++) {
$res = number_format($values[$i]);
var_dump($res);
echo '<br><br>';
}
echo "<br><br>WITH TWO DECIMAL POINTS:<br><br>";
for ($i = 0; $i < count($values); $i++) {
$res = number_format($values[$i], 2);
var_dump($res);
echo '<br><br>';
}
echo "<br><br>ENGLISH FORMAT:<br><br>";
for ($i = 0; $i < count($values); $i++) {
$res = number_format($values[$i], 2, '.', ' ');
var_dump($res);
echo '<br><br>';
}
echo "<br><br>FRENCH FORMAT:<br><br>";
for ($i = 0; $i < count($values); $i++) {
$res = number_format($values[$i], 2, ',' , ' ');
var_dump($res);
echo '<br><br>';
}
?>