php下一个阿拉伯数字转中文数字的函数
作者:bea
<?php functionch_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*)
<?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");
?>
有用 |
无用
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");
?>
猜你喜欢
您可能感兴趣的文章:
- 从一个不错的留言本弄的mysql数据库操作类
- 利用static实现表格的颜色隔行显示的代码
- MySQL数据库转移,access,sql server 转 MySQL 的图文教程
- Bo-Blog专用的给Windows服务器的IIS Rewrite程序
- 海河写的 Discuz论坛帖子调用js的php代码
- discuz 首页四格:最新话题+最新回复+热门话题+精华文章插件
- PHP下几种删除目录的方法总结
- wordpress之wp-settings.php
- PHP4和PHP5性能测试和对比 测试代码与环境
- 在PHP里得到前天和昨天的日期的代码
- PHP与MySQL开发中页面出现乱码的一种解决方法
- 真正的ZIP文件操作类(php)
- php 用sock技术发送邮件的函数
- php中ob(Output Buffer 输出缓冲)函数使用方法
- 在普通HTTP上安全地传输密码
- 发布一个迷你php+AJAX聊天程序[聊天室]提供下载
- PHP+FLASH实现上传文件进度条相关文件 下载
- Discuz5.5.0代码高亮显示+运行代码框合成插件 下载
- 一段php加密解密的代码