Many people who often access Big Data know that in order to read the size of a value more quickly, developers generally format the output large value: the general formatting method, it is useful in a format similar to xxx xxxx, but it is more common to add a half-width comma after each of the three numbers. This is also the most common format. Lower
Many people who often access Big Data know that in order to read the size of a value more quickly, developers generally format the output large value: the general formatting method, it is useful in a format similar to xxx xxxx, but it is more common to add a half-width comma after each of the three numbers. This is also the most common format. Lower
Many people who often access Big Data know that in order to read the size of a value more quickly, developers generally format the output large value: the general formatting method, it is useful in a format similar to xxx xxxx, but it is more common to add a half-width comma after each of the three numbers. This is also the most common format. Next, let's take a look at how to format a large value into the format of each three digits plus a comma. (In fact, when the number of comments on Wordpress reaches a certain level, this format is also used to format the values .)
Function number_format ($ num) {if (! Is_numeric ($ num) {// first judge the variable to see if it is a numeric or numeric string return false;} $ num = explode ('. ', $ num); // first, use the decimal point to divide the value into two parts: integer and decimal, and save it to the $ num variable $ rl = $ num [1]; // We all know that the decimal part is after the decimal point, so its key value is 1 $ j = strlen ($ num [0]) % 3; // next we divide the integer part by the three-digit remainder to determine the length of the first three digits: $ sl = substr ($ num [0], 0, $ j ); // then we use substr to calculate $ j from the first number, in fact, it is equivalent to truncating the first $ j number $ sr = substr ($ num [0], $ j); // you can extract the third part with the position exceeding (inclusive, that is, the integer part, excluding the remaining $ I = 0; while ($ I <= strlen ($ sr) {$ rvalue = $ Rvalue. ','. substr ($ sr, $ I, 3); // truncate the $ sr section in a three-way location and use a comma to connect them $ I = $ I + 3 ;} $ rvalue = $ sl. $ rvalue; // combine the partial portion of less than three digits with the subsequent portion $ rvalue = substr ($ rvalue, 0, strlen ($ rvalue)-1 ); // remove the last half-width comma of the integer $ rvalue = explode (',', $ rvalue ); // then separate the new numeric string with commas into an array if ($ rvalue [0] = 0) {array_shift ($ rvalue ); // if the first section is 0, delete it.} $ 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 ;}
In fact, explode and substr play a key role. The first one is to convert the numeric value or numeric string into an array, and the next one is responsible for intercepting !!
Statement: This article uses BY-NC-SA protocol for authorization | IT passers-
Reprinted please note