PHP Tutorial: format a number with a comma

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.