Home  >  Article  >  Backend Development  >  PHP implements RMB digital formatting, adding commas to every three digits

PHP implements RMB digital formatting, adding commas to every three digits

WBOY
WBOYOriginal
2016-07-25 08:45:261424browse
  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'];//The value of the decimal part
  8. $j = strlen($num[0] ) % 3;//How many digits are there in the integer
  9. $sl = substr($num[0], 0, $j);//Get the number with less than three digits in front of it
  10. $sr = substr($num[0], $j);//Take out the following three-digit number
  11. $i = 0;
  12. while($i <= strlen($sr)){
  13. $rvalue = $rvalue.','.substr($ sr, $i, 3);//Take out the three digits and combine them, 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);//Decompose into an 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];//The number with less than three digits in front
  23. for($i = 1 ; $i < count($rvalue); $i++){
  24. $rv = $rv.','.$rvalue[$i];
  25. }
  26. if(!empty($rl)){
  27. $rvalue = $rv.'.'.$rl;//The decimal is not empty, integers and decimals are combined
  28. }else{
  29. $rvalue = $rv;//The decimal is empty, only integers
  30. }
  31. return $rvalue;
  32. }
Copy code

PHP


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn