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;
    }
}
相关推荐
MaximusCoder8 小时前
等保测评命令——Anolis Linux
linux·运维·服务器·网络·经验分享·安全·php
Elastic 中国社区官方博客8 小时前
Elastic 为什么捐赠其 OpenTelemetry PHP 发行版
大数据·开发语言·elasticsearch·搜索引擎·信息可视化·全文检索·php
web3.088899914 小时前
使用PHP采集数据的完整技术文章,涵盖多种场景和最佳实践
开发语言·php
白帽子黑客-宝哥16 小时前
渗透测试“保姆级”实战成长手册
开发语言·网络安全·渗透测试·php
常利兵19 小时前
打造Android网络框架:为请求铺就高速路
android·网络·php
m0_7381207219 小时前
渗透测试——Tomato 靶场完整渗透思路(本地文件包含LFI,脏牛提取)
web安全·网络安全·php·安全性测试·webshell·蚁剑
scofield_gyb20 小时前
PHP进阶-在Ubuntu上搭建LAMP环境教程
开发语言·ubuntu·php
新缸中之脑20 小时前
用Gws+Valyu实现晨报自动化
运维·自动化·php
IT WorryFree1 天前
OpenClaw 对接飞书 Debug 指南
开发语言·php·飞书
Filotimo_1 天前
5.3 Internet基础知识
开发语言·php