Home  >  Article  >  Backend Development  >  How to format numbers with commas in php (code example)

How to format numbers with commas in php (code example)

藏色散人
藏色散人forward
2020-02-01 15:58:012166browse

How to format numbers with commas in php (code example)

Today’s work requires formatting numbers to display the current commodity price, such as 2335.32, which needs to be formatted as 2,335.32 to display like this. I wrote a function. I always feel that such a simple function requires more than 30 lines of code to complete.

The specific code is as follows:

 $len){
           $number_temp = substr($integer,0,($len - $pos)).$join.$number_temp;
           break;
       }
       $i++;
   }
    //处理小数部分
    if(!empty($decimal)){
        $number_temp = !empty($number_temp) ? $number_temp.".".$decimal : "0.".$decimal;
    }
    return $number_temp;
}
$result = numberFormat(1234567.5564);
print_r($result);
?>
//结果输出1,234,567.5564

For more PHP related knowledge, please visit php tutorial!

The above is the detailed content of How to format numbers with commas in php (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete