当前位置:网站首页>php中的数字金额转换大写数字
php中的数字金额转换大写数字
2022-07-02 06:23:00 【夜空の雪風】
前言
有时候需要进行数字金额与大写汉字金额的转换,网上虽然有很多的方法,但是大多都很混乱,这里整理一个现在在使用、感觉还不错的方法。
/** * 数字金额转换大写数字 * @param [float] $num 数字类型 * @return void */
function convert_case_number($num)
{
//转换数字类型
$num = (float) $num;
//判断$num是否存在
if (!$num) {
return '零圆';
}
//保留小数点后两位
$num = sprintf("%.2f", $num);
//将浮点转换为整数
$tem_num = $num * 100;
//判断数字长度
$tem_num_len = strlen($tem_num);
if ($tem_num_len > 14) {
return '数字过大';
}
//大写数字
$dint = array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
//大写金额单位
$danwei = array('仟', '佰', '拾', '亿', '仟', '佰', '拾', '万', '仟', '佰', '拾', '圆');
$danwei1 = array('角', '分');
//空的变量用来保存转换字符串
$daxie = '';
//分割数字,区分圆角分
list($left_num, $right_num) = explode('.', $num);
//计算单位长度
$danwei_len = count($danwei);
//计算分割后的字符串长度
$left_num_len = strlen($left_num);
$right_num_len = strlen($right_num);
//循环计算亿万元等
for ($i = 0; $i < $left_num_len; $i++) {
//循环单个文字
$key_ = substr($left_num, $i, 1);
//判断数字不等于0或数字等于0与金额单位为亿、万、圆,就返回完整单位的字符串
if ($key_ !== '0' || ($key_ == '0' && ($danwei[$danwei_len - $left_num_len + $i] == '亿' || $danwei[$danwei_len - $left_num_len + $i] == '万' || $danwei[$danwei_len - $left_num_len + $i] == '圆'))) {
$daxie = $daxie . $dint[$key_] . $danwei[$danwei_len - $left_num_len + $i];
} else {
//否则就不含单位
$daxie = $daxie . $dint[$key_];
}
}
//循环计算角分
for ($i = 0; $i < $right_num_len; $i++) {
$key_ = substr($right_num, $i, 1);
if ($key_ > 0) {
$daxie = $daxie . $dint[$key_] . $danwei1[$i];
}
}
//计算转换后的长度
$daxie_len = strlen($daxie);
//设置文字切片从0开始,utf-8汉字占3个字符
$j = 0;
while ($daxie_len > 0) {
//每次切片两个汉字
$str = substr($daxie, $j, 6);
//判断切片后的文字不等于零万、零圆、零亿、零零
if ($str == '零万' || $str == '零圆' || $str == '零亿' || $str == '零零') {
//重新切片
$left = substr($daxie, 0, $j);
$right = substr($daxie, $j + 3);
$daxie = $left . $right;
}
$j += 3;
$daxie_len -= 3;
}
return $daxie . '整';
}
例如:小写:172190.20元 转换成 大写:壹拾柒万贰仟壹佰玖拾圆贰角整
边栏推荐
- Code execution sequence with and without resolve in promise
- Sqli-labs customs clearance (less6-less14)
- Sqli labs customs clearance summary-page4
- Win10: add or delete boot items, and add user-defined boot files to boot items
- SQLI-LABS通关(less2-less5)
- Wechat applet Foundation
- sprintf_ How to use s
- 浏览器滚动加载更多实现
- Go package name
- table 组件指定列合并行方法
猜你喜欢
In depth study of JVM bottom layer (II): hotspot virtual machine object
CVE-2015-1635(MS15-034 )远程代码执行漏洞复现
There is no way to drag the win10 desktop icon (you can select it, open it, delete it, create it, etc., but you can't drag it)
Solution to the black screen of win computer screenshot
sqli-labs通关汇总-page3
由于不正常断电导致的unexpected inconsistency;RUN fsck MANUALLY问题已解决
Flex Jiugongge layout
Queue (linear structure)
PHP Session原理简析
ZZQ的博客目录--更新于20210601
随机推荐
SQL注入闭合判断
SQLI-LABS通关(less1)
[daily question] - Huawei machine test 01
js删除字符串的最后一个字符
How to try catch statements that return promise objects in JS
ZZQ的博客目录--更新于20210601
apt命令报证书错误 Certificate verification failed: The certificate is NOT trusted
Eslint configuration code auto format
Blog directory of zzq -- updated on 20210601
2021-07-05C#/CAD二次开发创建圆弧(4)
Error "list" object is not callable in Web automatic switching window
js创建一个自定义json数组
How to debug wechat built-in browser applications (enterprise number, official account, subscription number)
UEditor . Net version arbitrary file upload vulnerability recurrence
Win10: add or delete boot items, and add user-defined boot files to boot items
Sublime text configuring PHP compilation environment
Browser scrolling for more implementations
Stack (linear structure)
2020-9-23 use of QT timer qtimer class.
Deployment API_ automation_ Problems encountered during test