PHP function to convert any number into the corresponding Chinese number

/**
    * Change the number 1-100 million into Chinese characters, such as: 123->123
    * @param [num] $num [number]
    * @return [string] [string]
    */
    function numToWord($num)
    {
        $chiNum = array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine');
        $chiUni = array('','ten','hundred','thousand','million','billion','ten','hundred','thousand');

        $chiStr = '';

        $num_str = (string)$num;

        $count = strlen($num_str);
        $last_flag = true; //Whether the last one is 0
        $zero_flag = true; //Is it the first
        $temp_num = null; //temporary number

        $chiStr = '';//Splicing result
        if ($count == 2) {//two digits
            $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){
            $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 ++;
            }
        }else{
            $chiStr = $chiNum[$num_str[0]];
        }
        return $chiStr;
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325851646&siteId=291194637