This article mainly introduces PHP numbers every three comma-plus function function, want to know how to do the numbers every three comma friends can refer to the next. We hope to help you.
PHP implementation of digital format, the number of every three comma-plus function function, the specific code is as follows:
function Num_format ($num) { if (!is_numeric ($num)) { return false; } $num = Explode ('. ', $num);//separates integers from decimals $rl = $num [1];//fractional value $j = strlen ($num [0])% 3;//integer number of bits $SL = substr ($ Num[0], 0, $j);//The number of the preceding three digits is taken out $SR = substr ($num [0], $j);//The number of full three digits after//is taken out $i = 0; while ($i <= strlen ($SR)) { $rvalue = $rvalue. ', '. substr ($SR, $i, 3);//three-bit three-bit remove and merge, separated by commas $i = $i + 3; } $rvalue = $sl. $rvalue; $rvalue = substr ($rvalue, 0,strlen ($rvalue)-1);//Remove the last comma $rvalue = explode (', ', $rvalue);//Decompose the array if ($ rvalue[0]==0) { array_shift ($rvalue);//If the first element is 0, delete the first element } $rv = $rvalue [0];//front less than three digits for ($i = 1; $i < count ($rvalue); $i + +) { $rv = $rv. ', '. $rvalue [$i]; } if (!empty ($RL)) { $rvalue = $rv. '. '. $RL;//decimal is not empty, integer and decimal merge }else{ $rvalue = $RV;//Decimal is empty, only integer } return $rvalue;}
You can also use the system's own function string Number_format (float number [, int decimals [, String Dec_point, String thousands_sep]):
Example:
echo Number_format (' 169856420 ');
the output will be:169,856,420
echo Number_format (' 1000000 ', 2);
the output will be:1,000,000.00
echo Number_format (' 1000000 ', 2, ', ', '. ');
the output will be:1.000.000,00
Related recommendations:
PHP function usage method and function definition Method _php Tutorial
PHP encapsulated functions are not working properly, always prompt for parameter problems
PHP implementation Explort () function sample code