阿拉伯数字 转换 中文大写


阿拉伯数字 转换 中文大写


<?php
class ToChineseNumber {
    private $money = "";
    private $cnynums    = array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
    private $cnyunits   = array("圆","角","分");
    private $cnygrees   = array("拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟");
    function __construct($money=0){
        $this->money = round( $money , 2 );
    }
    private function cnyMapUnit( $list , $units ) {
        $ul     = count($units);
        $xs     = array();
        $list   = array_reverse($list);
        while( list($keys , $vals) = each($list) ){
            $l  = count($xs);
            if ($vals != "0" || !($l%4) ){
                $val = $vals . ( $units[ ($l-1)%$ul ] );
            }else{
                $val = is_numeric( $xs[0][0] ) ? $vals : '';
            }
            array_unshift( $xs , $val );
        }
        return $xs;
    }
    public function arr( $leng=0 , $suffix=false ){
        list( $int , $dec ) = explode( "." , $this->money , 2 );
        $cnygrees = $this->cnygrees;
        $cnynums = array_keys($this->cnynums);
        $temp   = array();
        $index  = 0;
        if( $leng > 0 && $suffix !== false ){
            for( $i=0; $i<$leng; $i++){ $cnygrees[$i] = $suffix; }
        }
        $data   = $this->cnyMapUnit( str_split($int) , $cnygrees );
        $data   = array_reverse( $data );
        while( list( $key , $val ) = each( $data ) ){
            $val = str_replace( $cnynums , $this->cnynums , $val );
            if( empty( $temp[ $index ] ) ){
                $temp[ $index ] = '';
            }
            $temp[ $index ] = $val . $temp[ $index ];
            $index++;
            if( $index > $leng ){
                $index = $leng;
            }
        }
        return array_reverse( $temp );
    }
}

$to = new ToChineseNumber( 1892600 );
$to->arr( 4 , '');
// Array
// (
//     [0] => 壹佰捌拾玖
//     [1] => 贰
//     [2] => 陆
//     [3] => 零
//     [4] => 零
// )

猜你喜欢

转载自www.cnblogs.com/thatme/p/10203052.html