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***76663 小时前
网络安全防护指南:筑牢网络安全防线(510)
安全·web安全·php
denggun123453 小时前
ios-WebP
macos·php·cocoa
悠悠~飘1 天前
17.PHP基础-数组
php
Q_Q5110082851 天前
python+django/flask+vue的书城图书阅读器系统,亮点含目录章节pycharm
spring boot·python·django·flask·node.js·php
计算机科学与技术学习中1 天前
文件上传漏洞
php
emma羊羊1 天前
【PHP反序列化】css夺旗赛
开发语言·网络安全·php
Q_Q5110082851 天前
python+django/flask的图书馆管理系统vue
spring boot·python·django·flask·node.js·php
Q_Q5110082851 天前
python+django/flask的美食交流宣传系统vue
spring boot·python·pycharm·django·flask·node.js·php
m0_495562782 天前
Swift的逃逸闭包
服务器·php·swift
Elastic 中国社区官方博客2 天前
Observability:适用于 PHP 的 OpenTelemetry:EDOT PHP 加入 OpenTelemetry 项目
大数据·开发语言·人工智能·elasticsearch·搜索引擎·全文检索·php