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

藏色散人
Release: 2023-03-05 15:12:01
Original
3406 people have browsed it

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;
}
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template