PHP numeric format. each three digits contain a comma, which can retain decimal places.

Source: Internet
Author: User
Tags float number
PHP numeric formatting. each three digits contain commas (,). decimals can be retained. to give the viewer a clearer and clearer number during quotation, you need to format the numbers in two ways, one is to write functions by myself, and the other is of course the built-in function. In fact, I prefer the built-in function .? First, the system is simple: stringnumber_format (float PHP number formatting. each three digits are added with a comma, and decimals can be retained.

In order to give the viewer a clearer and clearer number during the quote, you need to use number formatting. There are two methods: one is to write the function by yourself, and the other is of course the system's own, in fact, I prefer the built-in system.

?

First, the system is simple:

String number_format (float number [, int decimals [, string dec_point, string thousands_sep]):

?

View Code 1 echo number_format ('20140901 ');

?

Output result: 169,856,420

?

View Code 1 echo number_format ('20140901', 2 );

?

Output result: 1,000,000.00

?

View the code 1 echo number_format ('20140901', 2 ,',','.');

?

Output result: 1.000.000, 00

?

Let's look at the written functions:

?

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 ;}

??

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.