php数字转换为中文的类
时间:2007-02-17 来源:PHP爱好者
// 诸海加
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
// 2000-7-19
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
class ChineseNumber
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
{
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
var $basical=array(0=>"零","一","二","三","四","五","六","七","八","九");
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
//var $basical=array(0=>"零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
var $advanced=array(1=>"十","百","千");
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
//var $advanced=array(1=>"拾","佰","仟");
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
var $top=array(1=>"万","亿");
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
var $level; // 以4位为一级
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
// 先实现万一下的数的转换
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
function ParseNumber($number)
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
{
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if ($number>999999999999) // 只能处理到千亿。
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
return "数字太大,无法处理。抱歉!";
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if ($number==0)
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
return "零";
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
for($this->level=0;$number>0.0001;$this->level++,$number=floor($number / 10000))
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
{
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
// 对于中文来说,应该是4位为一组。
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
// 四个变量分别对应 个、十、百、千 位。
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$n1=substr($number,-1,1);
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if($number>9)
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$n2=substr($number,-2,1);
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
else
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$n2=0;
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if($number>99)
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$n3=substr($number,-3,1);
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
else
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$n3=0;
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if($number>999)
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$n4=substr($number,-4,1);
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
else
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$n4=0;
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if($n4)
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$parsed[$this->level].=$this->basical[$n4].$this->advanced[3];
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
else
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if(($number/10000)>=1) // 千位为0,数值大于9999的情况
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$parsed[$this->level].="零";
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if($n3)
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$parsed[$this->level].=$this->basical[$n3].$this->advanced[2];
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
else
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if(!ereg("零$",$parsed[$this->level]) && ($number / 1000)>=1) // 不出现连续两个“零”的情况
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$parsed[$this->level].="零";
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if($n2)
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$parsed[$this->level].=$this->basical[$n2].$this->advanced[1];
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
else
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if(!ereg("零$",$parsed[$this->level]) && ($number / 100)>=1) // 不出现连续两个“零”的情况
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$parsed[$this->level].="零";
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if($n1)
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$parsed[$this->level].=$this->basical[$n1];
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
}
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
for($this->level-=1;$this->level>=0;$this->level--)
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
{
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$result.=$parsed[$this->level].$this->top[$this->level];
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
}
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if(ereg("零$",$result))
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$result=substr($result,0,strlen($result)-2);
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
return $result;
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
}
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
};
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$c=new ChineseNumber();
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
echo $c->ParseNumber(123456789012);
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
?>
非常全面的一个php技术网站,php爱好者站 http://www.phpfans.net 有相当丰富的文章和源代码.
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
// 2000-7-19
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
class ChineseNumber
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
{
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
var $basical=array(0=>"零","一","二","三","四","五","六","七","八","九");
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
//var $basical=array(0=>"零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
var $advanced=array(1=>"十","百","千");
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
//var $advanced=array(1=>"拾","佰","仟");
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
var $top=array(1=>"万","亿");
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
var $level; // 以4位为一级
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
// 先实现万一下的数的转换
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
function ParseNumber($number)
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
{
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if ($number>999999999999) // 只能处理到千亿。
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
return "数字太大,无法处理。抱歉!";
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if ($number==0)
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
return "零";
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
for($this->level=0;$number>0.0001;$this->level++,$number=floor($number / 10000))
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
{
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
// 对于中文来说,应该是4位为一组。
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
// 四个变量分别对应 个、十、百、千 位。
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$n1=substr($number,-1,1);
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if($number>9)
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$n2=substr($number,-2,1);
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
else
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$n2=0;
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if($number>99)
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$n3=substr($number,-3,1);
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
else
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$n3=0;
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if($number>999)
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$n4=substr($number,-4,1);
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
else
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$n4=0;
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if($n4)
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$parsed[$this->level].=$this->basical[$n4].$this->advanced[3];
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
else
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if(($number/10000)>=1) // 千位为0,数值大于9999的情况
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$parsed[$this->level].="零";
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if($n3)
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$parsed[$this->level].=$this->basical[$n3].$this->advanced[2];
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
else
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if(!ereg("零$",$parsed[$this->level]) && ($number / 1000)>=1) // 不出现连续两个“零”的情况
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$parsed[$this->level].="零";
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if($n2)
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$parsed[$this->level].=$this->basical[$n2].$this->advanced[1];
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
else
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if(!ereg("零$",$parsed[$this->level]) && ($number / 100)>=1) // 不出现连续两个“零”的情况
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$parsed[$this->level].="零";
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if($n1)
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$parsed[$this->level].=$this->basical[$n1];
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
}
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
for($this->level-=1;$this->level>=0;$this->level--)
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
{
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$result.=$parsed[$this->level].$this->top[$this->level];
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
}
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
if(ereg("零$",$result))
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$result=substr($result,0,strlen($result)-2);
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
return $result;
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
}
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
};
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
$c=new ChineseNumber();
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
echo $c->ParseNumber(123456789012);
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
chinaitpower.comyFXrFIvPnDe0usjrUzzXRP4Mu
?>
非常全面的一个php技术网站,php爱好者站 http://www.phpfans.net 有相当丰富的文章和源代码.
相关阅读 更多 +