您现在的位置是:首页 > 数码 > 

php人民币函数,PHP数字转人民币大写函数

2025-07-19 01:44:31
php人民币函数,PHP数字转人民币大写函数 PHP数字转人民币大写函数2018-04-2 09:59 如果你是一位财务人员,肯定遇到过一个问题,就是遇到一串数字,要将其写成大写的人民币,有时很容易搞错。 那么有没有什么方法可以准确的解决呢? 当然是有的,今天分享的就是使用PHP将数字转人民币大写的函数。 p

php人民币函数,PHP数字转人民币大写函数

PHP数字转人民币大写函数2018-04-2 09:59

如果你是一位财务人员,肯定遇到过一个问题,就是遇到一串数字,要将其写成大写的人民币,有时很容易搞错。

那么有没有什么方法可以准确的解决呢?

当然是有的,今天分享的就是使用PHP将数字转人民币大写的函数。 php代码<?php

function rmb_format($money = 0, $is_round = true, $int_unit = 元 ) {

$chs     = array (0, 壹 , 贰 , 叁 , 肆 , 伍 , 陆 , 柒 , 捌 , 玖 );

$uni     = array ( , 拾 , 佰 , 仟 );

$dec_uni = array ( 角 , 分 );

$exp     = array ( , 万 , 亿 );

$res     = ;

// 以 元为单位分割

$parts   = explode ( . , $money, 2 );

$int     = isset ( $parts [0] ) ? strval ( $parts [0] ) : 0;

$dec     = isset ( $parts [1] ) ? strval ( $parts [1] ) : ;

// 处理小数点

$dec_len = strlen ( $dec );

if (isset ( $parts [1] ) && $dec_len > 2) {

$dec = $is_round ? substr ( strrchr ( strval ( round ( floatval ( 0. . $dec ), 2 ) ), . ), 1 ) : substr ( $parts [1], 0, 2 );

}

// number= 0.00时,直接返回 0

if (empty ( $int ) && empty ( $dec )) {

return 零 ;

}

// 整数部分 从右向左

for($i = strlen ( $int ) - 1, $t = 0; $i >= 0; $t) {

$str = ;

// 每4字为一段进行转化

for($j = 0; $j < 4 && $i >= 0; $j , $i --) {

$u   = $int{$i} > 0 ? $uni [$j] : ;

$str = $chs [$int {$i}] . $u . $str;

}

$str = rtrim ( $str, 0 );

$str = preg_replace ( /0/, 零, $str );

$u2  = $str != ? $exp [$t] : ;

$res = $str . $u2 . $res;

}

$dec = rtrim ( $dec, 0 );

// 小数部分 从左向右

if (!empty ( $dec )) {

$res .= $int_unit;

$cnt =  strlen ( $dec );

for($i = 0; $i < $cnt; $i ) {

$u = $dec {$i} > 0 ? $dec_uni [$i] : ; // 非0的数字后面添加单位

$res .= $chs [$dec {$i}] . $u;

}

if ($cnt == 1) $res .= 整 ;

$res = rtrim ( $res, 0 ); // 去掉末尾的0

$res = preg_replace ( /0/, 零, $res ); // 替换多个连续的0

} else {

$res .= $int_unit . 整 ;

}

return $res;

}

?>

$money为数字金额。

$is_round是否对小数进行四舍五入。

$int_unit为币种单位,默认是“元”,你也可以改成其他的,比如有些需求可能是“圆”。

实例

$money=287.6;

echo rmb_format($money);

//结果为:贰佰捌拾柒元叁角陆分

$money=128.19;

echo rmb_format($money,false);

//结果为:壹佰贰拾捌元壹角叁分

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/shuma/767854.html

相关标签:无
上传时间: 2023-12-27 15:02:26
留言与评论(共有 7 条评论)
本站网友 加盟投诉
16分钟前 发表
; // 处理小数点 $dec_len = strlen ( $dec ); if (isset ( $parts [1] ) && $dec_len > 2) { $dec = $is_round ? substr ( strrchr ( strval ( round ( floatval ( 0. . $dec )
本站网友 雷公藤的功效与作用
17分钟前 发表
仟 ); $dec_uni = array ( 角
本站网友 广安门
17分钟前 发表
2 ); } // number= 0.00时,直接返回 0 if (empty ( $int ) && empty ( $dec )) { return 零 ; } // 整数部分 从右向左 for($i = strlen ( $int ) - 1
本站网友 现在房价会跌吗
3分钟前 发表
仟 ); $dec_uni = array ( 角
本站网友 cpu降温圣手
3分钟前 发表
2 ); $int     = isset ( $parts [0] ) ? strval ( $parts [0] )
本站网友 血脂高吃什么好
4分钟前 发表
$is_round = true