PHP数字大小写转换(100以内和1亿以内)

PHP数字大小写转换,仅支持100以内,欢迎留言更好的算法

for($i = 0;$i<100;$i++){
echo $i.'=='.numberToBig($i);
echo PHP_EOL;
}


function numberToBig($number = 1){
    $bigArr = ['零','一','二','三','四','五','六','七','八','九'];

    if($number < 1){
       $result = '';
    }elseif($number < 10){
       $result = $bigArr[$number];
    }elseif($number % 10 !== 0 && $number < 100){
      $result = (floor($number / 10) > 1) ? $bigArr[floor($number / 10)] : '';
      $result .= '十' .$bigArr[$number % 10];
    }elseif($number % 10 === 0 && $number < 100){
      $result = (floor($number / 10) > 1) ? $bigArr[floor($number / 10)].'十' : '十';
    }else{
      $result = '';
    }
    return $result;
}
一亿以内数字转化汉字
function numToWord($num)
    {
        $chiNum = array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九');
        $chiUni = array('','十', '百', '千', '万', '十', '百', '千', '亿');
        $numUnit = array('','十', '百', '千', '万', '十万', '百万', '千万', '亿');

        $chiStr = '';

        $num_str = (string)$num;

        $count = strlen($num_str);
        $last_flag = true; //上一个 是否为0
        $zero_flag = true; //是否第一个
        $temp_num = null; //临时数字
        $flag  = false;

        $chiStr = '';//拼接结果
        if ($count == 2) {//两位数
            $temp_num = $num_str[0];
            $chiStr = $temp_num == 1 ? $chiUni[1] : $chiNum[$temp_num].$chiUni[1];
            $temp_num = $num_str[1];
            $chiStr .= $temp_num == 0 ? '' : $chiNum[$temp_num];
        }else if($count > 2 && $count < 6){
            $index = 0;
            for ($i=$count-1; $i >= 0 ; $i--) {
                $temp_num = $num_str[$i];
                if ($temp_num == 0) {
                    if (!$zero_flag && !$last_flag ) {
                        $chiStr = $chiNum[$temp_num]. $chiStr;
                        $last_flag = true;
                    }
                }else{
                    $chiStr = $chiNum[$temp_num].$chiUni[$index%9] .$chiStr;

                    $zero_flag = false;
                    $last_flag = false;
                }
                $index ++;
            }
        }elseif ($count > 5){
            $index = 0;
            for ($i=$count-1; $i >= 0 ; $i--) {
                $temp_num = $num_str[$i];
                
                if ($temp_num == 0) {
                    if (!$zero_flag && !$last_flag ) {
                        $chiStr = $chiNum[$temp_num]. $chiStr;
                        $last_flag = true;
                    }
                    
                }else{
                    
                      if($index > 3 && !$flag){
                            $chiStr = $chiNum[$temp_num].$numUnit[$index%9] .$chiStr;
$flag = true;
                      }else{
                    $chiStr = $chiNum[$temp_num].$chiUni[$index%9] .$chiStr;

                      }
 
                    $zero_flag = false;
                    $last_flag = false;
                }
                $index ++;
            }
        }else{
            $chiStr = $chiNum[$num_str[0]];
        }
        return $chiStr;
    }

//echo numToWord(1991009).PHP_EOL;exit;
for($i=10;$i<10000;$i++){
    $n = mt_rand(1,1000);
echo $n.'===>'.numToWord($n).PHP_EOL;
}

你可能感兴趣的:(PHP数字大小写转换(100以内和1亿以内))