PHP/Data Type/number format

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

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

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



Formatting Numbers As Text

 
<?php
print number_format(100000.56 );
?>



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

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



number_format for English format and Italian format

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



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

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



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

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



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

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



Printing a formatted number

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