PHP number format, number every three bits plus a comma, you can keep decimals

Source: Internet
Author: User
Tags float number
In order to give the visitors more clear and clear figures in the quotation, so need to use the number format, there are two methods, one of their own write function, the other of course is the system comes with, in fact, I prefer the system comes with.

First come the system simple:

String Number_format (float number [, int decimals [, String Dec_point, String thousands_sep]):

View Code 1 Echo number_format (' 169856420 ');

The output will be: 169,856,420

View Code 1 Echo number_format (' 1000000 ', 2);

The output will be: 1,000,000.0

View Code 1 Echo number_format (' 1000000 ', 2, ', ', '. ');

The output will be: 1.000.000,00

Again read the function:

function Num_format ($num) {if (!is_numeric ($num)) {return false; } $num = Explode ('. ', $num);//separates integers and decimals $rl = $num [value of 1];//fractional part $j = strlen ($num [0])% 3;//integer how many bits $sl = su       A BSTR ($num [0], 0, $j);//The number of the front less than three bits is taken out to $SR = substr ($num [0], $j);//The number of full three digits after it is taken out to $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 < Coun T ($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; }
  • 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.