php格式化数字的小例子 number_format函数的用法举例

发布时间:2020-03-25编辑:脚本学堂
本文介绍下,php中用于格式化数字的函数number_format的用法,举了几个小例子,供大家学习参考。

例1,格式化数字为文本
 

复制代码 代码示例:
<?php
print number_format(100000.56 );
?>

例2,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";
?>

例3,number_format函数用于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";
?>

例4,输出格式化的数字形式
 

复制代码 代码示例:
<?php
print "The population of the US is about:";
print number_format(285266237);
?>