php下实现一个阿拉伯数字转中文数字的函数
作者:bea
如果要用于金额的转换,对小数部分的处理要做一下修改 <?php functionch_num($num,$mode=true){ $char=array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖"); $dw=array("","拾","佰","仟","","萬","億","兆"); $dec="點"; $retval=""; if($mode) preg_match_all("/^0*(d*).?(d*)/",$num,$ar);
如果要用于金额的转换,对小数部分的处理要做一下修改
<?php
function ch_num($num,$mode=true) {
$char = array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
$dw = array("","拾","佰","仟","","萬","億","兆");
$dec = "點";
$retval = "";
if($mode)
preg_match_all("/^0*(d*).?(d*)/",$num, $ar);
else
preg_match_all("/(d*).?(d*)/",$num, $ar);
if($ar[2][0] != "")
$retval = $dec . ch_num($ar[2][0],false); //如果有小数,先递归处理小数
if($ar[1][0] != "") {
$str = strrev($ar[1][0]);
for($i=0;$i<strlen($str);$i++) {
$out[$i] = $char[$str[$i]];
if($mode) {
$out[$i] .= $str[$i] != "0"? $dw[$i%4] : "";
if($str[$i]+$str[$i-1] == 0)
$out[$i] = "";
if($i%4 == 0)
$out[$i] .= $dw[4+floor($i/4)];
}
}
$retval = join("",array_reverse($out)) . $retval;
}
return $retval;
}
//echo ch_num("12345006789001.123");
//echo ch_num("880079.1234");
echo ch_num("300045.0123");
?>
有用 |
无用
<?php
function ch_num($num,$mode=true) {
$char = array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
$dw = array("","拾","佰","仟","","萬","億","兆");
$dec = "點";
$retval = "";
if($mode)
preg_match_all("/^0*(d*).?(d*)/",$num, $ar);
else
preg_match_all("/(d*).?(d*)/",$num, $ar);
if($ar[2][0] != "")
$retval = $dec . ch_num($ar[2][0],false); //如果有小数,先递归处理小数
if($ar[1][0] != "") {
$str = strrev($ar[1][0]);
for($i=0;$i<strlen($str);$i++) {
$out[$i] = $char[$str[$i]];
if($mode) {
$out[$i] .= $str[$i] != "0"? $dw[$i%4] : "";
if($str[$i]+$str[$i-1] == 0)
$out[$i] = "";
if($i%4 == 0)
$out[$i] .= $dw[4+floor($i/4)];
}
}
$retval = join("",array_reverse($out)) . $retval;
}
return $retval;
}
//echo ch_num("12345006789001.123");
//echo ch_num("880079.1234");
echo ch_num("300045.0123");
?>
猜你喜欢
您可能感兴趣的文章:
- PHP4中session登录页面的应用
- PHP 表单提交给自己
- PHP 得到根目录的 __FILE__ 常量
- jq的get传参数在utf-8中乱码问题的解决php版
- 使用PHP socke 向指定页面提交数据
- php SQLite学习笔记与常见问题分析
- php sprintf()函数让你的sql操作更安全
- PHP版自动生成文章摘要
- php array_merge下进行数组合并的代码
- 用php过滤危险html代码的函数
- 高级php注入方法集锦
- php正则校验用户名介绍
- PHP批量生成缩略图的代码
- Discuz 模板引擎的封装类代码
- PHP令牌 Token改进版
- php下intval()和(int)转换使用与区别
- PHP入门学习的几个不错的实例代码
- php通用检测函数集合
- php header()函数使用说明