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;
    }
}
相关推荐
专注前端30年6 小时前
【PHP开发与安全防护实战】性能调优手册
android·安全·php
oMcLin7 小时前
如何在 RHEL 7 上优化 Nginx 与 PHP‑FPM 配置,确保高并发 Web 应用的稳定性与响应速度?
前端·nginx·php
IT=>小脑虎10 小时前
PHP零基础衔接进阶知识点【详解版】
开发语言·学习·php
xifangge202511 小时前
PHP 接口跨域调试完整解决方案附源码(从 0 到定位问题)
开发语言·php
ICT董老师12 小时前
通过kubernetes部署nginx + php网站环境
运维·nginx·云原生·容器·kubernetes·php
bleach-12 小时前
buuctf系列解题思路祥讲--[SUCTF 2019]CheckIn1--文件上传以及user.ini的应用
nginx·web安全·网络安全·php
BingoGo14 小时前
免费可商用商业级管理后台 CatchAdmin V5 正式发布 插件化与开发效率的全面提升
vue.js·后端·php
AI 智能服务1 天前
第6课__本地工具调用(文件操作)
服务器·人工智能·windows·php
松涛和鸣1 天前
49、智能电源箱项目技术栈解析
服务器·c语言·开发语言·http·html·php
晚枫歌F1 天前
io_uring的介绍和实现
开发语言·php