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;
    }
}
相关推荐
用户7227868123449 小时前
PHP 初学者指南 基础结构与常见错误
php
爱潜水的小L9 小时前
自学嵌入式day37,网络编程
开发语言·网络·php
风月歌11 小时前
php医院预约挂号系统小程序源代码(源码+文档+数据库)
数据库·微信小程序·小程序·毕业设计·php·源码
计算机学姐11 小时前
基于php的非物质文化遗产推广系统
开发语言·vue.js·mysql·tomcat·php·postman
松涛和鸣12 小时前
37、UDP网络编程入门
linux·服务器·前端·网络·udp·php
万岳软件开发小城13 小时前
在线教育系统源码开发技术解析:课程、直播、考试与多端适配方案
开源·php·在线教育系统源码·教育小程序·教育平台搭建·教育软件开发·在线教育app开发
知识中的海王13 小时前
cloudflare email 邮箱混淆/加密/解密 PHP 源码版
开发语言·php
新鲜势力呀14 小时前
TensorFlow 中 tf.placeholder 适用版本解析|附 PHP 调用 TF 模型实战(兼容低版本)
tensorflow·php·neo4j
catchadmin14 小时前
当遇见 CatchAdmin V5-模块化设计重新定义 Laravel 后台开发
php·laravel
BingoGo14 小时前
PHP 初学者指南 基础结构与常见错误
后端·php