PHP implements code that separates large numbers with , as a delimiter

WBOY
Release: 2016-07-25 09:07:16
Original
912 people have browsed it
  1. //Floating point type is not considered
  2. /*
  3. * method 1
  4. * echo number_format($str,2,'.',',');
  5. */
  6. /* method2
  7. * First reverse the string strrev and then str_split($str,3);
  8. $str = strrev($str);
  9. $arr = str_split($str,3);//987
  10. $res = '';
  11. $count = count($arr);
  12. while($count--){
  13. $res .= strrev($arr[$count]).',';
  14. }
  15. $res = rtrim($res,' ,');
  16. */
  17. /* method 3
  18. * Cut out every 3 characters
  19. * $count = strlen($str);
  20. $i = 0;
  21. $md = $count % 3;
  22. switch ($ md){
  23. case 0:
  24. break;
  25. case 1:
  26. $res = $str{0}.',';
  27. $count -=1;
  28. $i = 1;
  29. break;
  30. case 2:
  31. $ res = substr($str,0,2).',';
  32. $count -= 2;
  33. $i = 2;
  34. break;
  35. }
  36. for(;$i<$count-3;$i+=3 ){
  37. $res .= substr($str,$i,3).',';
  38. }
  39. $res .= substr($str,$i,3);
  40. */
  41. /* method 4
  42. * Regular way to find uncertain length
  43. $md = strlen($str) % 3;
  44. $res = substr($str, 0,$md).($md == 0?'':',');
  45. $res .= preg_replace('(d{3})', '\0,', substr($str, $md));
  46. $res = rtrim($res,',');
  47. */
  48. ?>
Copy code


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!