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;
    }
}
相关推荐
狂野小青年6 小时前
在PHP Web开发中,实现异步处理有几种常见方式的优缺点,以及最佳实践推荐方法
消息队列·php·最佳实践·异步任务
张鱼小丸子10 小时前
【无标题】云原生作业六
开发语言·php
哥坐11路1 天前
网络IP跳动问题解决详
开发语言·php
一只哒布刘1 天前
第六次作业
开发语言·php
寰宇软件1 天前
PHP房屋出租出售高效预约系统小程序源码
前端·小程序·uni-app·vue·php
HUNAG-DA-PAO1 天前
Redis存在线程安全吗?为什么?
redis·安全·php
ianozo1 天前
BUU40 [安洵杯 2019]easy_serialize_php
android·开发语言·php
zgscwxd1 天前
php session数据存储位置选择
开发语言·php
ianozo2 天前
CTF 代码学习日记 PHP
java·学习·php
ontheway-xx2 天前
PHP+Apache+MySQL安装(Windows)
开发语言·php