Function of adding a comma to each three digits of php, and the three-digit comma function of php. The function of adding a comma to each three digits in php. The three-digit comma function of php is used to format a number. the Function of adding a comma to each three digits in php is as follows: functionnum_format
Php allows you to format numbers. the code is as follows:
Function num_format ($ num) {if (! Is_numeric ($ num) {return false;} $ num = explode ('. ', $ num); // separate integers from decimals $ rl = $ num [1]; // the decimal part value $ j = strlen ($ num [0]) % 3; // Number of digits in the integer $ sl = substr ($ num [0], 0, $ j ); // Obtain the number of less than three digits in the front $ sr = substr ($ num [0], $ j); // Obtain the number of the full three digits in the back $ I = 0; while ($ I <= strlen ($ sr) {$ rvalue = $ rvalue. ','. substr ($ sr, $ I, 3); // three digits are extracted and then merged, separated by commas $ I = $ I + 3;} $ rvalue = $ sl. $ rvalue; $ rvalue = substr ($ rvalue, 0, strlen ($ rvalue)-1); // remove the last comma $ r Value = explode (',', $ rvalue); // break it into an array if ($ rvalue [0] = 0) {array_shift ($ rvalue ); // if the first element is 0, delete the first element} $ rv = $ rvalue [0]; // for ($ I = 1; $ I <count ($ rvalue); $ I ++) {$ rv = $ rv. ','. $ rvalue [$ I];} if (! Empty ($ rl) {$ rvalue = $ rv. '. '. $ rl; // The decimal place is not empty. the integer and decimal places are merged.} else {$ rvalue = $ rv; // The decimal place is empty. only the integer} return $ rvalue ;}
You can also use the built-in functions.String number_format (float number [, int decimals [, string dec_point, string thousands_sep]):
Example:
echo number_format('169856420');
The output result is as follows:169,856,420
echo number_format('1000000',2);
The output result is as follows:1,000,000.00
echo number_format('1000000',2,',','.');
The output result is as follows:1.000.000, 00
The above is the code for php to implement three digits plus commas. at the beginning, we can see whether there is no idea about the question. I hope this article will give you some inspiration.
The numeric formatting function is implemented in php. the code is as follows: function num_format ($ n...