- function num_format($num){
- if(!is_numeric($num)){
- return false;
- }
- $rvalue='';
- $num = explode('.',$num);/ /Separate integers and decimals
- $rl = !isset($num['1']) ? '' : $num['1'];//The value of the decimal part
- $j = strlen($num[0] ) % 3;//How many digits are there in the integer
- $sl = substr($num[0], 0, $j);//Get the number with less than three digits in front of it
- $sr = substr($num[0], $j);//Take out the following three-digit number
- $i = 0;
- while($i <= strlen($sr)){
- $rvalue = $rvalue.','.substr($ sr, $i, 3);//Take out the three digits and combine them, separated by commas
- $i = $i + 3;
- }
- $rvalue = $sl.$rvalue;
- $rvalue = substr($ rvalue,0,strlen($rvalue)-1);//Remove the last comma
- $rvalue = explode(',',$rvalue);//Decompose into an array
- if($rvalue[0]==0) {
- array_shift($rvalue);//If the first element is 0, delete the first element
- }
- $rv = $rvalue[0];//The number with less than three digits in front
- for($i = 1 ; $i < count($rvalue); $i++){
- $rv = $rv.','.$rvalue[$i];
- }
- if(!empty($rl)){
- $rvalue = $rv.'.'.$rl;//The decimal is not empty, integers and decimals are combined
- }else{
- $rvalue = $rv;//The decimal is empty, only integers
- }
- return $rvalue;
- }
Copy code
|