PHPIN.NET

 找回密码
 立即注册
查看: 4435|回复: 0

[PHP类\函数] PHP数字金额转换大写金额

[复制链接]

469

主题

31

回帖

5509

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
5509
发表于 2021-5-22 20:48:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
PHP数字金额转换大写金额


  1. <?php
  2. /**
  3. * @Author: MaXian
  4. * @Date:   2021-05-22 20:47:43
  5. * @Last Modified by:   MaXian
  6. * @Last Modified time: 2021-05-22 20:47:43
  7. */
  8. /**
  9. * 将数值金额转换为中文大写金额
  10. * @param $amount float 金额(支持到分)
  11. * @param $type   int   补整类型,0:到角补整;1:到元补整
  12. * @return mixed 中文大写金额
  13. */
  14. function convertAmountToCn($amount, $type = 1) {
  15.         // 判断输出的金额是否为数字或数字字符串
  16.         if(!is_numeric($amount)){
  17.                 return "要转换的金额只能为数字!";
  18.         }

  19.         // 金额为0,则直接输出"零元整"
  20.         if($amount == 0) {
  21.                 return "人民币零元整";
  22.         }

  23.         // 金额不能为负数
  24.         if($amount < 0) {
  25.                 return "要转换的金额不能为负数!";
  26.         }

  27.         // 金额不能超过万亿,即12位
  28.         if(strlen($amount) > 12) {
  29.                 return "要转换的金额不能为万亿及更高金额!";
  30.         }

  31.         // 预定义中文转换的数组
  32.         $digital = array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
  33.         // 预定义单位转换的数组
  34.         $position = array('仟', '佰', '拾', '亿', '仟', '佰', '拾', '万', '仟', '佰', '拾', '元');

  35.         // 将金额的数值字符串拆分成数组
  36.         $amountArr = explode('.', $amount);

  37.         // 将整数位的数值字符串拆分成数组
  38.         $integerArr = str_split($amountArr[0], 1);

  39.         // 将整数部分替换成大写汉字
  40.         $result = '人民币';
  41.         $integerArrLength = count($integerArr);     // 整数位数组的长度
  42.         $positionLength = count($position);         // 单位数组的长度
  43.         $zeroCount = 0;                             // 连续为0数量
  44.         for($i = 0; $i < $integerArrLength; $i++) {
  45.                 // 如果数值不为0,则正常转换
  46.                 if($integerArr[$i] != 0){
  47.                         // 如果前面数字为0需要增加一个零
  48.                         if($zeroCount >= 1){
  49.                                 $result .= $digital[0];
  50.                         }
  51.                         $result .= $digital[$integerArr[$i]] . $position[$positionLength - $integerArrLength + $i];
  52.                         $zeroCount = 0;
  53.                 }else{
  54.                         $zeroCount += 1;
  55.                         // 如果数值为0, 且单位是亿,万,元这三个的时候,则直接显示单位
  56.                         if(($positionLength - $integerArrLength + $i + 1)%4 == 0){
  57.                                 $result = $result . $position[$positionLength - $integerArrLength + $i];
  58.                         }
  59.                 }
  60.         }

  61.         // 如果小数位也要转换
  62.         if($type == 0) {
  63.                 // 将小数位的数值字符串拆分成数组
  64.                 $decimalArr = str_split($amountArr[1], 1);
  65.                 // 将角替换成大写汉字. 如果为0,则不替换
  66.                 if($decimalArr[0] != 0){
  67.                         $result = $result . $digital[$decimalArr[0]] . '角';
  68.                 }
  69.                 // 将分替换成大写汉字. 如果为0,则不替换
  70.                 if($decimalArr[1] != 0){
  71.                         $result = $result . $digital[$decimalArr[1]] . '分';
  72.                 }
  73.         }else{
  74.                 $result = $result . '整';
  75.         }
  76.         return $result;
  77. }
复制代码

防止编辑器转换代码 提供一个附件:
PHP数字金额转换大写金额.php (2.7 KB, 下载次数: 0)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|PHPIN.NET ( 冀ICP备12000898号-14 )|网站地图

GMT+8, 2024-4-24 04:27

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表