PHP/Data Type/number format

Материал из Web эксперт
Перейти к: навигация, поиск

Format an integer and a floating-point value with the number_format() function.

   <source lang="html4strict">

<?php $i = 123456; $f = 98765.567; $si = number_format($i, 0, ",", "."); $sf = number_format($f, 2); echo "\$si = $si and \$sf = $sf\n"; ?>

 </source>
   
  


Formatting Numbers As Text

   <source lang="html4strict">

<?php print number_format(100000.56 ); ?>

 </source>
   
  


number_format() enables us to determine the precision we require using a second argument: an integer.

   <source lang="html4strict">

<?php print number_format (100000.56, 2 ); print number_format(100000.56, 4 ); ?>

 </source>
   
  


number_format for English format and Italian format

   <source lang="html4strict">

<?php

   $a = 1232322210.44;
   echo number_format ($a, 2);   // English format
   echo "\n";
   echo number_format ($a, 2, ",", "."); // Italian format
   echo "\n";

?>

 </source>
   
  


number_format($n,$p) rounds $n to $p decimal places, adding commas between thousands

   <source lang="html4strict">

<?

   echo "Total charge is \$", number_format($total, 2), "\n";

?>

 </source>
   
  


number_format($n, $p, $t, $d) rounds $n to $p decimal places, using $t as the thousands separator and $d as the decimal separator.

   <source lang="html4strict">

<?

   echo "Total charge is ", number_format($total, 2, ".", ","), " Euros";

?>

 </source>
   
  


number_format: the second parameter indicates the number of decimals after the decimal point

   <source lang="html4strict">

<?php $i = 123456; $f = 98765.567; $si = number_format($i, 0, ",", "."); $sf = number_format($f, 2); echo "\$si = $si and \$sf = $sf\n"; ?>

 </source>
   
  


Printing a formatted number

   <source lang="html4strict">

<?php print "The population of the US is about:"; print number_format(285266237); ?>

 </source>