number_format 


string apg

FORMATS a number whit grouped thousands.





This function accepts ONE, TWO or FOUR parameters, never three..

Although the FLOAT type is requested for $number 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.

This function is not compatible with the LOCALE



<?php

str number_format 
( float $number [, 
                                    
int $decimals = 0 ] )


where,

$number = The NUMBER to be formated

$decimals 
= To set the number of decimal points

?>

$number


The NUMBER to be formated.



$decimals


To set the number of decimal points.

The default value is $decimals = 0.



<?php

str number_format 
( float $number ,
                                   
int $decimals = 0 , 
                                   
str $dec_point = "." , 
                                   
str $thousands_sep = "," )


where,

$number = The NUMBER to be formated

$decimals 
= To set the number of decimal points

$dec_point 
= To set the separator for decimal point

$thousands_sep 
= To set the thousands separator

?>

$number


The NUMBER to be formated.



$decimals


To set the number of decimal points.

The default value is $decimals = 0 .



$dec_point


To set the separator for decimal points.

The default value is $dec_point = "," .



$thousands_sep


To set the thousands separator.

The default value is $thousands_sep = "." .



  1 EXERCISE   

<?php

 
// STRING
$nbr01s = '1234567';

// INTEGER
$nbr01n = 123456;

// AMERICAN LOCALE
setlocale(LC_ALL, "en_US", "en_US");
 

echo 
'LOCALE: "en_US"<br><br>';
echo 
'STRING<br><br>';
$nbr01se = number_format($nbr01s, 8);
echo 
'<b>' . $nbr01se . '</b><br><br>';

echo 
'FLOAT<br><br>';
$nbr01ne = number_format($nbr01n, 8);
echo 
'<b>' . $nbr01ne . '</b><br><br><br>';


// BRAZILIAN LOCALE
setlocale(LC_ALL, "pt_BR.utf-8", "pt-BR");

echo 
'LOCALE: "pt_BR"<br><br>';
echo 
'STRING<br><br>';
$nbr01sb = number_format($nbr01s, 8);
echo 
'<b>' . $nbr01sb . '</b><br><br>';

echo 
'FLOAT<br><br>';
$nbr01nb = number_format($nbr01n, 8);
echo 
'<b>' . $nbr01nb . '</b>';

?>

 RESULT   

A) Initial value as STRING

'1234567'

B) Initial value as INTEGER

1234567


LOCALE: "en_US"

A) Treated as a STRING

1,234,567.00000000

B) Treated as an INTEGER

123,456.00000000


LOCALE: "pt_BR"

A) Treated as a STRING

1,234,567.00000000

B) Treated as an INTEGER

123,456.00000000



You should note that in this case, the indication LOCALE is perfectly unnecessary.

  2 EXERCISE   

<?php

echo 'INPUT FORMATTING as STRING:<br>';

 
// STRING
$nbr01s = "12345.6789";

echo 
'"'. $nbr01s . '"<br><br><br>';

echo 
"ZERO decimal places, ONE parameter:<br>";
$frnbr01s = number_format($nbr01s);
echo 
'number_format("' . $nbr01s . '") -> '  . 
                                       
$frnbr01s . '<br><br>';

echo 
"TWO decimal places, TWO parameters:<br>";
$frnbr02s = number_format($nbr01s , 2);
echo 
'number_format("' . $nbr01s . '", ' . 2 . ') -> '  . 
                                       
$frnbr02s . '<br><br>';

echo 
"EIGHT decimal places, TWO parameters:<br>";
$frnbr08s = number_format($nbr01s , 8);
echo 
'number_format("' . $nbr01s . '", ' . 8 . ') -> '  . 
                                       
$frnbr08s . '<br><br><br><br>';

echo 
"NINE decimal places, FOUR parameters<br>
                             French format I:<br>"
;
$frnbr09s = number_format($nbr01s , 9, ',', '.');
echo 
'number_format("' . $nbr01s . '", ' . 9 . ', \',\', \'.\') -> '  . 
                                       
$frnbr09s . '<br><br>';

echo 
"NINE decimal places, FOUR parameters<br>
                              French format II:<br>"
;
$frnbr09as = number_format($nbr01s , 9, ',', '');
echo 
'number_format("' . $nbr01s . '", ' . 9 . ', \',\', \'\') -> '  . 
                                       
$frnbr09as . '<br>';

?>

 RESULT   

INPUT FORMATTING as STRING:
"12345.6789"


ZERO decimal places, ONE parameter:
number_format("12345.6789") -> 12,346

TWO decimal places, TWO parameters:
number_format("12345.6789", 2) -> 12,345.68

EIGHT decimal places, TWO parameters:
number_format("12345.6789", 8) -> 12,345.67890000



NINE decimal places, FOUR parameters
French format I:
number_format("12345.6789", 9, ',', '.') -> 12.345,678900000

NINE decimal places, FOUR parameters
French format II:
number_format("12345.6789", 9, ',', '') -> 12345,678900000


  3 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 'STRING<br><br>';
echo 
'"' . $nbr011s . '"<br><br><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 French format I 
$frnbr091s = number_format($nbr011s , 9, ',', '.');
echo 
'number_format("' . $nbr011s . '", ' . 9 . ', \',\', \'.\') ->'  . 
                                       
$frnbr091s . '<br><br><br>';

 
// nine decimal places, four parameters French format II
$frnbr091as = number_format($nbr011s , 9, ',', '');
echo 
'number_format("' . $nbr011s . '", ' . 9 . ', \',\', \'\') ->'  . 
                                       
$frnbr091as . '<br><br><br>';

?>

  4 EXERCISE   

<?php

$nbr01b 
= 12345.6789;
echo 
'FLOAT<br><br>';
echo 
'' . $nbr01b . '<br><br><br>';

$frnbr01b = number_format($nbr01b);
echo 
'number_format(' . $nbr01b . ') -> '  . 
                                     
$frnbr01b . '<br><br><br>';

$frnbr02b = number_format($nbr01b , 2);
echo 
'number_format(' . $nbr01b . ', ' . 2 . ') -> '  . 
                                     
$frnbr02b . '<br><br><br>';

$frnbr08b = number_format($nbr01b , 8);
echo 
'number_format("' . $nbr01b . '", ' . 8 . ') -> '  . 
                                     
$frnbr08b . '<br><br><br>';

$frnbr09b = number_format($nbr01b , 9, ',', '.');
echo 
'number_format(' . $nbr01b . ', ' . 9 . ', \',\', \'.\') ->'  . 
                                     
$frnbr09b . '<br><br><br>';

$frnbr09c = number_format($nbr01b , 9, ',', '');

echo 
'number_format(' . $nbr01b . ', ' . 9 . ', \',\', \'\') ->'  . 
                                     
$frnbr09c . '<br><br><br>';

?>

  5 EXERCISE   

<?php

$values 
= array(1234.5678,
                -
1234.5678,
                
1234.6578e4,
                -
1234.56789e4,
                
0x1234CDEF,
                
02777777777,
                
"123456789",
                
"123.456789",
                
"12.3456789e1",
                
null,
                
true,
                
false);

echo 
"<br><br>number_format tests.....default:<br>";
for (
$i = 0; $i < count($values); $i++) {
    
$res = number_format($values[$i]);
    
var_dump($res);
    echo 
'<br>';
}

echo 
"<br><br>number_format tests.....with two dp:<br>";
for (
$i = 0; $i < count($values); $i++) {
    
$res = number_format($values[$i], 2);
    
var_dump($res);
    echo 
'<br>';
}

echo 
"<br><br>number_format tests.....English format:<br>";
for (
$i = 0; $i < count($values); $i++) {
    
$res = number_format($values[$i], 2, '.', ' ');
    
var_dump($res);
    echo 
'<br>';
}

echo 
"<br><br>number_format tests.....French format:<br>";
for (
$i = 0; $i < count($values); $i++) {
    
$res = number_format($values[$i], 2, ',' , ' ');
    
var_dump($res);
    echo 
'<br>';
}

?>

  6 EXERCISE   

<?php

echo "Testing number_format() : basic functionality.<br>";

$values = array(1234.5678,
                -
1234.5678,
                
1234.6578e4,
                -
1234.56789e4,
                
0x1234CDEF,
                
02777777777,
                
"123456789",
                
"123.456789",
                
"12.3456789e1",
                
null,
                
true,
                
false);

echo 
"<br><br>number_format tests.....default:<br>";
for (
$i = 0; $i < count($values); $i++) {
    
$res = number_format($values[$i]);
    
var_dump($res);
    echo 
'<br>';
}

echo 
"<br><br>number_format tests.....with two dp:<br>";
for (
$i = 0; $i < count($values); $i++) {
    
$res = number_format($values[$i], 2);
    
var_dump($res);
    echo 
'<br>';
}

echo 
"<br><br>number_format tests.....English format:<br>";
for (
$i = 0; $i < count($values); $i++) {
    
$res = number_format($values[$i], 2, '.', ' ');
    
var_dump($res);
    echo 
'<br>';
}

echo 
"<br><br>number_format tests.....French format:<br>";
for (
$i = 0; $i < count($values); $i++) {
    
$res = number_format($values[$i], 2, ',' , ' ');
    
var_dump($res);
    echo 
'<br>';
}

?>

  7 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>";
for (
$i = 0; $i < count($values); $i++) {
    
$res = number_format($values[$i], 2, '&#183;', ' ');
    
var_dump($res);
    echo 
'<br>';
}

echo 
"<br><br>Multiple character thousand separator:<br>";
for (
$i = 0; $i < count($values); $i++) {
    
$res = number_format($values[$i], 2, '.' , '&thinsp;');
    
var_dump($res);
    echo 
'<br>';
}

echo 
"<br><br>Multiple character decimal and thousep:<br>";
for (
$i = 0; $i < count($values); $i++) {
    
$res = number_format($values[$i], 2, '&#183;' , '&thinsp;');
    
var_dump($res);
    echo 
'<br>';
}

?>

  8 EXERCISE   

<?php

$number 
= -1.15E-15;

var_dump($number);
echo 
'<br><br>';
var_dump(number_format($number, 2));
echo 
'<br>';
var_dump(number_format(-0.01, 2));

?>

  9 EXERCISE   

<?php

// Different results in PHP 7 and PHP 8

$number = 2020.1415;

echo 
"Given: $number<br><br>";

var_dump(number_format($number, 2, 'F'));

?>

  10 EXERCISE   

<?php

$number 
= 2020.1415;

echo 
"Given: $number<br><br>";

var_dump(number_format($number, 2, null, 'T'));
echo 
'<br><br>';
var_dump(number_format($number, 2, 'F', null));

?>