Home >Backend Development >PHP Problem >How to convert Arabic numerals to uppercase letters in php

How to convert Arabic numerals to uppercase letters in php

藏色散人
藏色散人Original
2020-09-01 10:48:093409browse

php Method to convert Arabic numerals to uppercase: first create a PHP sample file; then define a "getChineseNumber" method; then match numbers through the "preg_match_all" function; and finally convert through the getChineseNumber method.

How to convert Arabic numerals to uppercase letters in php

Recommendation: "PHP Video Tutorial"

php Method to convert Arabic numerals to uppercase:

php realizes the conversion of Arabic numerals to Chinese capitals

/**
     *  阿拉伯数字到中文大写转换
     *
     * @param $num
     * @param bool $mode
     * @return string
     */
    private function getChineseNumber($num, $mode = true)
    {
        $char = array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖");
        $dw = array("", "拾", "佰", "仟", "", "万", "亿", "兆");
        $dec = "点";
        $retval = "";
        if ($mode) {
            preg_match_all("/^0*(\d*)\.?(\d*)/", $num, $ar);
        } else {
            preg_match_all("/(\d*)\.?(\d*)/", $num, $ar);
        }
 
        if ($ar[2][0] != 00) {
            $retval = $dec . $this->getChineseNumber($ar[2][0], false); //如果有小数,则用递归处理小数
        }
 
        if ($ar[1][0] != 00) {
            $str = strrev($ar[1][0]);
            for ($i = 0; $i < strlen($str); $i++) {
                $out[$i] = $char[$str[$i]];
                if ($mode) {
                    $out[$i] .= $str[$i] != "0" ? $dw[$i % 4] : "";
 
                    if ($str[$i] + $str[($i - 1 >= 0) ? $i - 1 : 0] == 0) {
                        $out[$i] = "";
                    }
 
                    if ($i % 4 == 0) {
                        $out[$i] .= $dw[4 + floor($i / 4)];
                    }
                }
            }
 
            $retval = join("", array_reverse($out)) . $retval;
        }
 
        return $retval;
}

The above is the detailed content of How to convert Arabic numerals to uppercase letters in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn