ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
~~~ <?php /**** * @author Amos Wang * @param $number * @return mixed */ function numberFormat($number) { if(!is_numeric($number)){ return $number; // 只处理数字 } list($integer,$decimal) = explode('.',$number); $number_temp = ""; // 临时计数 $len = strlen($integer); $sublen = 3; // 每隔几位数加逗号 //处理整数部分 $i=1; while(true){ $pos = $i * $sublen; $join = empty($number_temp) ? "" : ","; // 连接符 $number_temp = substr($integer,(0-$pos),$sublen).$join.$number_temp; if(($pos + $sublen) > $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 ~~~