PHP/Data Type/number format
Содержание
- 1 Format an integer and a floating-point value with the number_format() function.
- 2 Formatting Numbers As Text
- 3 number_format() enables us to determine the precision we require using a second argument: an integer.
- 4 number_format for English format and Italian format
- 5 number_format($n,$p) rounds $n to $p decimal places, adding commas between thousands
- 6 number_format($n, $p, $t, $d) rounds $n to $p decimal places, using $t as the thousands separator and $d as the decimal separator.
- 7 number_format: the second parameter indicates the number of decimals after the decimal point
- 8 Printing a formatted number
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>