·您的位置: 首页 » 资源教程 » 编程开发 » PHP » 数字转换为中文的类。(今晚的无聊之作)

数字转换为中文的类。(今晚的无聊之作)

类别: PHP教程  评论数:0 总得分:0
<?php
// 诸海加
// 2000-7-19

class ChineseNumber
{
    var $basical=array(0=>"零","一","二","三","四","五","六","七","八","九");
    //var $basical=array(0=>"零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
    var $advanced=array(1=>"十","百","千");
    //var $advanced=array(1=>"拾","佰","仟");
    var $top=array(1=>"万","亿");

    var $level;            // 以4位为一级

    // 先实现万一下的数的转换
    function ParseNumber($number)
    {
        if ($number>999999999999)        // 只能处理到千亿。
            return "数字太大,无法处理。抱歉!";
        if ($number==0)
            return "零";

        for($this->level=0;$number>0.0001;$this->level++,$number=floor($number / 10000))
        {
            // 对于中文来说,应该是4位为一组。
            // 四个变量分别对应 个、十、百、千 位。
            $n1=substr($number,-1,1);
            if($number>9)
                $n2=substr($number,-2,1);
            else
                $n2=0;
            if($number>99)
                $n3=substr($number,-3,1);
            else
                $n3=0;
            if($number>999)
                $n4=substr($number,-4,1);
            else
                $n4=0;

            if($n4)
                $parsed[$this->level].=$this->basical[$n4].$this->advanced[3];
            else
                if(($number/10000)>=1)        // 千位为0,数值大于9999的情况
                    $parsed[$this->level].="零";
            if($n3)
                $parsed[$this->level].=$this->basical[$n3].$this->advanced[2];
            else
                if(!ereg("零$",$parsed[$this->level]) && ($number / 1000)>=1)    // 不出现连续两个“零”的情况
                    $parsed[$this->level].="零";
            if($n2)
                $parsed[$this->level].=$this->basical[$n2].$this->advanced[1];
            else
                if(!ereg("零$",$parsed[$this->level]) && ($number / 100)>=1)    // 不出现连续两个“零”的情况
                    $parsed[$this->level].="零";
            if($n1)
                $parsed[$this->level].=$this->basical[$n1];
        }
        for($this->level-=1;$this->level>=0;$this->level--)
        {
            $result.=$parsed[$this->level].$this->top[$this->level];
        }

        if(ereg("零$",$result))
            $result=substr($result,0,strlen($result)-2);

        return $result;
        
    }
};

$c=new ChineseNumber();
echo $c->ParseNumber(123456789012);

?>


-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1