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;
    }
}
相关推荐
神仙别闹3 小时前
基于PHP+MySQL实现在线考试系统
开发语言·mysql·php
棒棒的唐4 小时前
配置 VSCode 的 PHP Intelephense 插件,去掉因php版本不同导至的红色波浪线误判
ide·vscode·php
Mike117.5 小时前
GBase 8a DBLink 查询的落地边界和排查细节
开发语言·php
ooseabiscuit6 小时前
PHP与C++:Web与系统编程的终极对决
前端·c++·php
ooseabiscuit16 小时前
Laravel 8.x核心特性深度解析
php·laravel
求学中--1 天前
鸿蒙网络请求从入门到精通:HttpURLConnection+第三方库,GET/POST/文件上传全覆盖
开发语言·php·harmonyos
yaodong5181 天前
Gemini长上下文重塑RAG架构
开发语言·php
fengci.1 天前
CTF+随机困难部分
android·开发语言·网络·安全·php
自不量力的A同学1 天前
PHP 8.5.6 发布
开发语言·php
Web极客码1 天前
2026年自建网站实战指南:从服务器到上线的完整方案
运维·服务器·php