当前位置:Gxlcms > PHP教程 > 数字金额转换成中文大写金额的PHP函数

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

时间:2021-07-01 10:21:17 帮助过:28人阅读

  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 < $slen) {
  40. $m = substr($c, $j, 6);
  41. if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
  42. $left = substr($c, 0, $j);
  43. $right = substr($c, $j + 3);
  44. $c = $left . $right;
  45. $j = $j-3;
  46. $slen = $slen-3;
  47. }
  48. $j = $j + 3;
  49. }
  50. if (substr($c, strlen($c)-3, 3) == '零') {
  51. $c = substr($c, 0, strlen($c)-3);
  52. }
  53. if (empty($c)) {
  54. return "零元整";
  55. }else{
  56. return $c . "整";
  57. }
  58. }


转换成, PHP

人气教程排行