欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

数字金额转换成中文大写金额的PHP函数

程序员文章站 2022-06-11 12:15:24
...
  1. /**
  2. *数字金额转换成中文大写金额的函数
  3. *String Int $num 要转换的小写数字或小写字符串
  4. *return 大写字母
  5. *小数位为两位
  6. **/
  7. function get_amount($num){
  8. $c1 = "零壹贰叁肆伍陆柒捌玖";
  9. $c2 = "分角元拾佰仟万拾佰仟亿";
  10. $num = round($num, 2);
  11. $num = $num * 100;
  12. if (strlen($num) > 10) {
  13. return "数据太长,没有这么大的钱吧,检查下";
  14. }
  15. $i = 0;
  16. $c = "";
  17. while (1) {
  18. if ($i == 0) {
  19. $n = substr($num, strlen($num)-1, 1);
  20. } else {
  21. $n = $num % 10;
  22. }
  23. $p1 = substr($c1, 3 * $n, 3);
  24. $p2 = substr($c2, 3 * $i, 3);
  25. if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
  26. $c = $p1 . $p2 . $c;
  27. } else {
  28. $c = $p1 . $c;
  29. }
  30. $i = $i + 1;
  31. $num = $num / 10;
  32. $num = (int)$num;
  33. if ($num == 0) {
  34. break;
  35. }
  36. }
  37. $j = 0;
  38. $slen = strlen($c);
  39. while ($j $m = substr($c, $j, 6);
  40. if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
  41. $left = substr($c, 0, $j);
  42. $right = substr($c, $j + 3);
  43. $c = $left . $right;
  44. $j = $j-3;
  45. $slen = $slen-3;
  46. }
  47. $j = $j + 3;
  48. }
  49. if (substr($c, strlen($c)-3, 3) == '零') {
  50. $c = substr($c, 0, strlen($c)-3);
  51. }
  52. if (empty($c)) {
  53. return "零元整";
  54. }else{
  55. return $c . "整";
  56. }
  57. }
复制代码


转换成, PHP