Blogger Information
Blog 28
fans 0
comment 2
visits 52100
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数字金额转大写文字
耶和华信徒的博客
Original
1410 people have browsed it

php数字金额转大写文字函数,输入需要转换的金额,返回对应的文字

  1. function num2upper($money, $type = false)
  2. {
  3. $result = '';
  4. $money = strrev( (string)($money*100) );
  5. $number = $type ? ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
  6. : ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
  7. $unit = $type ? ['分','角','元']
  8. : ['分','角','圆'];
  9. $unit2 = $type ? ['千', '万', '十', '百'] : ['仟', '万', '拾', '佰'];
  10. //$unit2 = $type ? ['千', '万', '十', '百'] : ['拾', '佰' ,'仟', '万', ];
  11. $money = (string)$money;
  12. /** - - - - - - - 简单错误处理 - - - - - - **/
  13. if ($money > 99999999999) return '金额过大,无法显示'; //最大值错误提醒;
  14. if ($money == 0) return '零元整'; //0元简单处理;
  15. if ($money < 0) return '金额错误,请联系管理员'; //最大值错误提醒;
  16. $lenght = strlen($money);
  17. for ($i = 0; $i < $lenght; $i++) {
  18. $tmp = '';
  19. // if ( $i != 0 && ($i < $lenght-1)最大位不可能为0 && $money[$i] == 0 && $money[$i+1] == 0) continue ;//跳过多个零
  20. $tmp .= $number[ $money[$i] ]; //数字替换成文字
  21. if ($i < 3) { //小额处理(元角分)
  22. $tmp .= $unit[ $i ];
  23. }else {
  24. //分位不验证 当前数为零 下一位还是0
  25. if ( $i != 0 && $money[$i] == 0 && $money[$i+1] == 0) {
  26. continue ;//跳过多个零
  27. }
  28. if ( $money[$i] != 0 && $i != 10) $tmp .= $unit2[($i-1)%4]; //特殊处理亿元
  29. else if ( $i == 6 ) $tmp .= '万';
  30. else if ( $i == 10 ) $tmp .= '亿';
  31. }
  32. $result = $tmp . $result; //向前将金额文字排列
  33. }
  34. $result = str_replace('零角零分', '整', $result); //添加整字
  35. $result = str_replace('零零圆', '圆', $result); //添加圆字
  36. $result = str_replace('零圆', '圆', $result); //添加圆字
  37. $result = str_replace('零零', '零', $result); //添加整字
  38. $result = str_replace('零万', '万零', $result); //添加整字
  39. $result = str_replace('零分', '', $result); //去掉零分
  40. return $result;
  41. }
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
1 comments
china_新之助 2021-07-20 17:11:22
为什么没有人喷我
1 floor
Author's latest blog post