PHP implementation of RMB digital format, every three bits plus commas

Source: Internet
Author: User
  1. function Num_format ($num) {
  2. if (!is_numeric ($num)) {
  3. return false;
  4. }
  5. $rvalue = ";
  6. $num = Explode ('. ', $num);//separate integers and decimals
  7. $RL =!isset ($num [' 1 '])? ": $num [' 1 '];//value of the decimal part
  8. $j = strlen ($num [0])% 3;//integer number of bits
  9. $SL = substr ($num [0], 0, $j);//The number of the front less than three bits is taken out
  10. $SR = substr ($num [0], $j);//The number of full three digits after it is taken out
  11. $i = 0;
  12. while ($i <= strlen ($SR)) {
  13. $rvalue = $rvalue. ', '. substr ($SR, $i, 3);//three-bit three-bit remove and merge, separated by commas
  14. $i = $i + 3;
  15. }
  16. $rvalue = $sl. $rvalue;
  17. $rvalue = substr ($rvalue, 0,strlen ($rvalue)-1);//Remove the last comma
  18. $rvalue = Explode (', ', $rvalue);//decomposition of the array
  19. if ($rvalue [0]==0) {
  20. Array_shift ($rvalue);//If the first element is 0, delete the first element
  21. }
  22. $RV = $rvalue [0];//in front of three digits
  23. for ($i = 1; $i < count ($rvalue); $i + +) {
  24. $RV = $rv. ', '. $rvalue [$i];
  25. }
  26. if (!empty ($RL)) {
  27. $rvalue = $rv. '. '. $RL;//decimals are not empty, integers and decimals are merged
  28. }else{
  29. $rvalue = $RV;//Decimal is empty, only integer
  30. }
  31. return $rvalue;
  32. }
Copy Code
Php
  • 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.