第一种
/*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
$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("582368.2576");
echo "
";
echo @ch_num("6935.2058");//输出结果为:陆仟玖佰叁拾伍點贰零伍捌
*/
第二种
header("Content-Type:text/html;charset=utf-8");
function cny($ns) {
static $cnums=array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖"),
$cnyunits=array("圆","角","分"),
$grees=array("拾","佰","仟","万","拾","佰","仟","亿");
list($ns1,$ns2)=explode(".",$ns,2);
$ns2=array_filter(array($ns2[1],$ns2[0]));
$ret=array_merge($ns2,array(implode("",_cny_map_unit(str_split($ns1),$grees)),""));
$ret=implode("",array_reverse(_cny_map_unit($ret,$cnyunits)));
return str_replace(array_keys($cnums),$cnums,$ret);
}
function _cny_map_unit($list,$units) {
$ul=count($units);
$xs=array();
foreach (array_reverse($list) as $x) {
$l=count($xs);
if ($x!="0" || !($l%4)) $n=($x=='0'?"":$x).($units[($l-1)%$ul]);
else $n=is_numeric($xs[0][0])?$x:"";
array_unshift($xs,$n);
}
return $xs;
}
echo @cny('150.25');//输出结果为壹佰伍拾圆贰角伍分
第三种
/**
* 人民币小写转大写
*
* @param string $number 数值
* @param string $int_unit 币种单位,默认"元",有的需求可能为"圆"
* @param bool $is_round 是否对小数进行四舍五入
* @param bool $is_extra_zero 是否对整数部分以0结尾,小数存在的数字附加0,比如1960.30,
* 有的系统要求输出"壹仟玖佰陆拾元零叁角",实际上"壹仟玖佰陆拾元叁角"也是对的
* @return string
*/
function num2rmb($number = 0, $int_unit = '元', $is_round = TRUE, $is_extra_zero = FALSE)
{
// 将数字切分成两段
$parts = explode('.', $number, 2);
$int = isset($parts[0]) ? strval($parts[0]) : '0';
$dec = isset($parts[1]) ? strval($parts[1]) : '';
// 如果小数点后多于2位,不四舍五入就直接截,否则就处理
$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.001时,小数点后的金额为0元
if(empty($int) && empty($dec))
{
return '零';
}
// 定义
$chs = array('0','壹','贰','叁','肆','伍','陆','柒','捌','玖');
$uni = array('','拾','佰','仟');
$dec_uni = array('角', '分');
$exp = array('', '万');
$res = '';
// 整数部分从右向左找
for($i = strlen($int) - 1, $k = 0; $i >= 0; $k++)
{
$str = '';
// 按照中文读写习惯,每4个字为一段进行转化,i一直在减
for($j = 0; $j < 4 && $i >= 0; $j++, $i--)
{
$u = $int{$i} > 0 ? $uni[$j] : ''; // 非0的数字后面添加单位
$str = $chs[$int{$i}] . $u . $str;
}
//echo $str."|".($k - 2)."
";
$str = rtrim($str, '0');// 去掉末尾的0
$str = preg_replace("/0+/", "零", $str); // 替换多个连续的0
if(!isset($exp[$k]))
{
$exp[$k] = $exp[$k - 2] . '亿'; // 构建单位
}
$u2 = $str != '' ? $exp[$k] : '';
$res = $str . $u2 . $res;
}
// 如果小数部分处理完之后是00,需要处理下
$dec = rtrim($dec, '0');
// 小数部分从左向右找
if(!empty($dec))
{
$res .= $int_unit;
// 是否要在整数部分以0结尾的数字后附加0,有的系统有这要求
if ($is_extra_zero)
{
if (substr($int, -1) === '0')
{
$res.= '零';
}
}
for($i = 0, $cnt = strlen($dec); $i < $cnt; $i++)
{
$u = $dec{$i} > 0 ? $dec_uni[$i] : ''; // 非0的数字后面添加单位
$res .= $chs[$dec{$i}] . $u;
}
$res = rtrim($res, '0');// 去掉末尾的0
$res = preg_replace("/0+/", "零", $res); // 替换多个连续的0
}
else
{
$res .= $int_unit . '整';
}
return $res;
}
echo "
";
$number = "1000000000000000012345678900.501";
echo $number.":".num2rmb($number);
echo "\n";
$number = "1960.30";
echo $number.":".num2rmb($number);
echo "\n";
$number = "1960.30";
echo $number.":".num2rmb($number, "圆", true, true);
echo "\n";
$number = "123456789.005";
echo $number.":".num2rmb($number);
echo "\n";
$number = "123456789.005";
echo $number.":".num2rmb($number, "元", false);
echo "\n";
$number = "10000000000000000060009.101";
echo $number.":".num2rmb($number);
echo "\n";
$number = "1680.32";
echo $number.":".num2rmb($number);
/*输出结果对照
1000000000000000012345678900.501:壹仟亿亿亿零壹佰贰拾叁亿肆仟伍佰陆拾柒万捌仟玖佰元伍角
1960.30:壹仟玖佰陆拾元叁角
1960.30:壹仟玖佰陆拾圆零叁角
123456789.005:壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖元零壹分
123456789.005:壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖元整
10000000000000000060009.101:壹佰万亿亿零陆万零玖元壹角
1680.32:壹仟陆佰捌拾元叁角贰分 */