人民币数字和中文汉字转换

在PHP中,将人民币的中文汉字金额转换为数字,或者将数字转换为人民币的中文汉字金额,通常需要自定义一些函数来实现这一转换过程。下面分别给出这两个转换的示例代码。

数字转人民币中文汉字

php 复制代码
function numberToChinese($num) {  
    $cnNums = array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');  
    $cnUnits = array('', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿');  
    $numStr = strval($num);  
    $numStr = array_reverse(str_split($numStr));  
    $result = '';  
    $zeroCount = 0;  
      
    for ($i = 0; $i < count($numStr); $i++) {  
        $digit = intval($numStr[$i]);  
        $unit = $cnUnits[$i % 4];  
          
        if ($digit === 0) {  
            $zeroCount++;  
        } else {  
            if ($zeroCount > 0) {  
                $result = $cnNums[0] . $result;  
            }  
            $zeroCount = 0;  
            $result = $cnNums[$digit] . $unit . $result;  
        }  
          
        if (($i + 1) % 4 === 0 && $digit !== 0) {  
            $result = $cnUnits[4 + intval(($i + 1) / 4) - 1] . $result;  
        }  
    }  
      
    $result = trim($result, $cnNums[0]); // 去除结果字符串两边的零  
    if (empty($result)) {  
        $result = $cnNums[0]; // 如果结果为空,则返回零  
    }  
      
    return '人民币' . $result . '元整';  
}  
  
// 示例用法  
echo numberToChinese(123456789); // 输出:人民币壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖元整

人民币中文汉字转数字

php 复制代码
function chineseToNumber($chinese) {  
    // 去除人民币和元整等字样  
    $chinese = preg_replace('/人民币|元整|元正|元|整|正/i', '', $chinese);  
    $cnNums = array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');  
    $cnUnits = array('', '拾', '佰', '仟');  
    $unitPosition = array(1, 10, 100, 1000); // 个、十、百、千  
    $num = 0;  
    $position = 0; // 万位、亿位等的位置,初始为个位  
    $isPrevZero = false; // 前一个数字是否为零  
  
    $chinese = str_split($chinese);  
    for ($i = 0; $i < count($chinese); $i++) {  
        $char = $chinese[$i];  
        if (in_array($char, $cnNums)) {  
            $digit = array_search($char, $cnNums);  
            if ($digit === 0 && $isPrevZero) {  
                continue; // 连续的零只计算一个  
            }  
            $num += $digit * $unitPosition[$i % 4] * pow(10000, intval($position));  
            $isPrevZero = ($digit === 0);  
        } elseif (in_array($char, $cnUnits)) {  
            if ($char === '万' || $char === '亿') {  
                $position++;  
            }  
        }  
    }  
  
    return $num;  
}  
  
// 示例用法  
echo chineseToNumber('人民币壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖元整'); // 输出:123456789
相关推荐
向日葵.5 小时前
fastdds.ignore_local_endpoints 属性
服务器·网络·php
dog25011 小时前
难以超越的 TCP AIMD
网络协议·tcp/ip·php
檀越剑指大厂12 小时前
【Linux系列】如何在 Linux 服务器上快速获取公网
linux·服务器·php
Q_Q51100828515 小时前
python的软件工程与项目管理课程组学习系统
spring boot·python·django·flask·node.js·php·软件工程
BingoGo16 小时前
重新学习 PHP 目前短运算符 简化你得代码
后端·php
Fine姐18 小时前
The Network Link Layer: 无线传感器中Delay Tolerant Networks – DTNs 延迟容忍网络
开发语言·网络·php·硬件架构
hotlinhao1 天前
php版的FormCreate使用注意事项
php·crmeb
鱼鱼说测试1 天前
Jenkins+Python自动化持续集成详细教程
开发语言·servlet·php
网硕互联的小客服1 天前
Apache 如何支持SHTML(SSI)的配置方法
运维·服务器·网络·windows·php
苏琢玉1 天前
如何让同事自己查数据?写一个零依赖 PHP SQL 查询工具就够了
mysql·php