php将数字转为中文汉字

记录:php将数字转为中文汉字

php 复制代码
if (!function_exists('num_to_cn_money')) {
    // 金额数字转汉字
    function num_to_cn_money($num, $mode = true, $sim = false){
        if (!is_numeric($num)) return '含有非数字非小数点字符!';
        $char = $sim ? array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九')
            : array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
        $unit = $sim ? array('', '十', '百', '千', '', '万', '亿', '兆')
            : array('', '拾', '佰', '仟', '', '萬', '億', '兆');
        $retval = $mode ? '元' : '点';
        //小数部分
        if (strpos($num, '.')) {
            list($num, $dec) = explode('.', $num);
            $dec = strval(round($dec, 2));
            if ($mode) {
                $retval .= "{$char[$dec['0']]}角{$char[$dec['1'] ?? 0]}分";
            } else {
                for ($i = 0, $c = strlen($dec); $i < $c; $i++) {
                    $retval .= $char[$dec[$i]];
                }
            }
        }
        //整数部分
        $str = $mode ? strrev(intval($num)) : strrev($num);
        for ($i = 0, $c = strlen($str); $i < $c; $i++) {
            $out[$i] = $char[$str[$i]];
            if ($mode) {
                $out[$i] .= $str[$i] != '0' ? $unit[$i % 4] : '';
                if ($i > 1 and $str[$i] + $str[$i - 1] == 0) {
                    $out[$i] = '';
                }
                if ($i % 4 == 0) {
                    $out[$i] .= $unit[4 + floor($i / 4)];
                }
            }
        }
        $retval = join('', array_reverse($out)) . $retval;
        return $retval;
    }
}
相关推荐
q***428213 小时前
IPV6公网暴露下的OPENWRT防火墙安全设置(只允许访问局域网中指定服务器指定端口其余拒绝)
服务器·安全·php
BingoGo14 小时前
# 9 个步骤教你如何安全地迁移数据库或字段
后端·php
流浪猪头拯救地球15 小时前
利用 Python 解密 / 加密 PDF 文件
python·pdf·php
JaguarJack15 小时前
9 个步骤教你如何安全地迁移数据库或字段
php·服务端
Jtti17 小时前
PHP项目缓存占用硬盘过大?目录清理与优化
java·缓存·php
catchadmin17 小时前
使用 PHP 和 Raylib 也可以开发贪吃蛇游戏
开发语言·游戏·php
地衣君17 小时前
Wordpress 插件 TOC+(Table of Contents Plus) 不支持多路径生成目录的 bug 修复
php·wordpress
拾忆,想起19 小时前
Dubbo超时问题排查与调优指南:从根因到解决方案
服务器·开发语言·网络·微服务·架构·php·dubbo
wuyoula1 天前
旧物回收系统源码 – go语言版
php·源码·源码分享·网站源码·源码大全
w***48821 天前
网络安全防护指南:筑牢网络安全防线(510)
安全·web安全·php