zoukankan      html  css  js  c++  java
  • PHP数字金额转换大写金额

    白俊遥博客

    20190303, 今天在博客园看到有文章在讲"PHP数字金额转换大写金额", 于是我抽时间也写了一个. 不多说, 把代码发上来: 

     1 /**
     2  * 将数值金额转换为中文大写金额
     3  * @param $amount float 金额(分)
     4  * @param $type  int   补整类型,0:到角补整;1:到元补整
     5  * @return mixed 中文大写金额
     6  */
     7 function convertAmountToCn($amount, $type = 1) {
     8     if ($amount == 0) {
     9         return "零元整";
    10     }
    11     if (strlen($amount) > 12) {
    12         return "不支持万亿及更高金额";
    13     }
    14      
    15     // 预定义中文转换的数组
    16     $digital = array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
    17     // 预定义单位转换的数组
    18     $position = array('仟', '佰', '拾', '亿', '仟', '佰', '拾', '万', '仟', '佰', '拾', '元');
    19      
    20     // 将金额的数值字符串拆分成数组
    21     $amountArr = explode('.', $amount);
    22      
    23     // 将整数位的数值字符串拆分成数组
    24     $integerArr = str_split($amountArr[0], 1);
    25     // 将整数部分替换成大写汉字
    26     $result = '人民币';
    27     $integerArrLength = count($integerArr);
    28     $positionLength = count($position);
    29     for($i=0; $i<$integerArrLength; $i++){
    30         $result = $result . $digital[$integerArr[$i]]. $position[$positionLength - $integerArrLength + $i];
    31     }
    32      
    33     // 如果小数位也要转换
    34     if($type == 1){
    35         // 将小数位的数值字符串拆分成数组
    36         $decimalArr = str_split($amountArr[1], 1);
    37         // 将小数部分替换成大写汉字
    38         $result = $result . $digital[$decimalArr[0]] . '角' . $digital[$decimalArr[1]] . '分';
    39     }else{
    40         $result = $result . '整';
    41     }
    42      
    43     return $result;
    44 }

    20190305, 经过考虑后, 发现之前写的代码不够严谨, 特此补充了一份新版的代码

     1 /**
     2  * 将数值金额转换为中文大写金额
     3  * @param $amount float 金额(支持到分)
     4  * @param $type   int   补整类型,0:到角补整;1:到元补整
     5  * @return mixed 中文大写金额
     6  */
     7 function convertAmountToCn($amount, $type = 1) {
     8     // 判断输出的金额是否为数字或数字字符串
     9     if(!is_numeric($amount)){
    10         return "要转换的金额只能为数字!";
    11     }
    12 
    13     // 金额为0,则直接输出"零元整"
    14     if($amount == 0) {
    15         return "人民币零元整";
    16     }
    17 
    18     // 金额不能为负数
    19     if($amount < 0) {
    20         return "要转换的金额不能为负数!";
    21     }
    22 
    23     // 金额不能超过万亿,即12位
    24     if(strlen($amount) > 12) {
    25         return "要转换的金额不能为万亿及更高金额!";
    26     }
    27 
    28     // 预定义中文转换的数组
    29     $digital = array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
    30     // 预定义单位转换的数组
    31     $position = array('仟', '佰', '拾', '亿', '仟', '佰', '拾', '万', '仟', '佰', '拾', '元');
    32 
    33     // 将金额的数值字符串拆分成数组
    34     $amountArr = explode('.', $amount);
    35 
    36     // 将整数位的数值字符串拆分成数组
    37     $integerArr = str_split($amountArr[0], 1);
    38 
    39     // 将整数部分替换成大写汉字
    40     $result = '人民币';
    41     $integerArrLength = count($integerArr);     // 整数位数组的长度
    42     $positionLength = count($position);         // 单位数组的长度
    43     for($i = 0; $i < $integerArrLength; $i++) {
    44         // 如果数值不为0,则正常转换
    45         if($integerArr[$i] != 0){
    46             $result = $result . $digital[$integerArr[$i]] . $position[$positionLength - $integerArrLength + $i];
    47         }else{
    48             // 如果数值为0, 且单位是亿,万,元这三个的时候,则直接显示单位
    49             if(($positionLength - $integerArrLength + $i + 1)%4 == 0){
    50                 $result = $result . $position[$positionLength - $integerArrLength + $i];
    51             }
    52         }
    53     }
    54 
    55     // 如果小数位也要转换
    56     if($type == 0) {
    57         // 将小数位的数值字符串拆分成数组
    58         $decimalArr = str_split($amountArr[1], 1);
    59         // 将角替换成大写汉字. 如果为0,则不替换
    60         if($decimalArr[0] != 0){
    61             $result = $result . $digital[$decimalArr[0]] . '角';
    62         }
    63         // 将分替换成大写汉字. 如果为0,则不替换
    64         if($decimalArr[1] != 0){
    65             $result = $result . $digital[$decimalArr[1]] . '分';
    66         }
    67     }else{
    68         $result = $result . '整';
    69     }
    70     return $result;
    71 }

    本文原创自老刘博客 http://laoliu.pro/article/27

  • 相关阅读:
    Web开发(二)HTML
    Web开发(一)基础
    Python基础(十二)并发编程02
    Python基础(十一)并发编程01
    Python基础(十)socket编程
    Python基础(九)异常
    计算机基础
    Python基础(八)面向对象02
    jmeter使用正则表达式提取数据
    jmeter+ant 实现自动化接口测试环境配置
  • 原文地址:https://www.cnblogs.com/mclaoliu/p/10468865.html
Copyright © 2011-2022 走看看