PHP数目字格式化,数字每三位加逗号,可以保留小数

WBOY
Release: 2016-06-13 10:41:15
Original
749 people have browsed it

PHP数字格式化,数字每三位加逗号,可以保留小数

在报价的时候为了给浏览者更清晰明确的数字,所以需要用到数字格式化,有两种方法,一种自己写函数,另一种当然是系统自带的,其实我更喜欢系统自带的。

?

先来系统简单的:

string number_format ( float number [, int decimals [, string dec_point, string thousands_sep]] ):

?

查看代码1 echo number_format('169856420');

?

输出结果将为:169,856,420

?

查看代码1 echo number_format('1000000',2);

?

输出结果将为:1,000,000.00

?

查看代码1 echo number_format('1000000',2,',','.');

?

输出结果将为:1.000.000,00

?

再看写的函数:

?

function num_format($num){     if(!is_numeric($num)){         return false;       }       $num = explode('.',$num);//把整数和小数分开     $rl = $num[1];//小数部分的值     $j = strlen($num[0]) % 3;//整数有多少位      $sl = substr($num[0], 0, $j);//前面不满三位的数取出来      $sr = substr($num[0], $j);//后面的满三位的数取出来      $i = 0;       while($i <p>??</p><div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template