Home > Backend Development > PHP Tutorial > PHP number conversion to uppercase function method tutorial

PHP number conversion to uppercase function method tutorial

小云云
Release: 2023-03-17 21:28:01
Original
1548 people have browsed it

In this article, we mainly share the PHP function method for converting numbers into uppercase letters. When we write a payment bill, we need to fill in the uppercase of the numerical amount. To be honest, I really can’t write it out if I don’t usually write it. In order to avoid embarrassing scenes in the future, I even went out of my way to practice those ten words. Haha, of course, we can also convert in PHP, so we have the following conversion function:

/**
 * 金额的小写转大写
 * @param $ns int 输入的数字
 */

function cny($ns) {
    static $cnums = array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"),
    $cnyunits = array("圆", "角", "分"),
    $grees = array("拾", "佰", "仟", "万", "拾", "佰", "仟", "亿");
    list($ns1, $ns2) = explode(".", $ns, 2);
    $ns2 = array_filter(array($ns2[1], $ns2[0]));
    $ret = array_merge($ns2, array(implode("", _cny_map_unit(str_split($ns1), $grees)), ""));
    $ret = implode("", array_reverse(_cny_map_unit($ret, $cnyunitss)));
    return str_replace(array_keys($cnums), $cnums, $ret);
}

function _cny_map_unit($list, $units) {
    $ul = count($units);
    $xs = array();
    foreach (array_reverse($list) as $x) {
        $l = count($xs);
        if ($x != "0" || !($l % 4))
            $n = ($x == '0' ? '' : $x) . ($units[($l - 1) % $ul]);
        else
            $n = is_numeric($xs[0][0]) ? $x : '';
        array_unshift($xs, $n);
    }
    return $xs;
}
Copy after login
Copy after login

View original text>> Mamba Children's Shoes - Blog - PHP Number Conversion to Uppercase Function


When we write to pay a bill, we need to write the numerical amount in capital letters. To be honest, I really can’t write it out if I don’t usually do it. In order to avoid embarrassing scenes in the future, I specially practice it. Those ten words. Haha, of course, we can also convert in php, so we have the following conversion function:

/**
 * 金额的小写转大写
 * @param $ns int 输入的数字
 */

function cny($ns) {
    static $cnums = array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"),
    $cnyunits = array("圆", "角", "分"),
    $grees = array("拾", "佰", "仟", "万", "拾", "佰", "仟", "亿");
    list($ns1, $ns2) = explode(".", $ns, 2);
    $ns2 = array_filter(array($ns2[1], $ns2[0]));
    $ret = array_merge($ns2, array(implode("", _cny_map_unit(str_split($ns1), $grees)), ""));
    $ret = implode("", array_reverse(_cny_map_unit($ret, $cnyunitss)));
    return str_replace(array_keys($cnums), $cnums, $ret);
}

function _cny_map_unit($list, $units) {
    $ul = count($units);
    $xs = array();
    foreach (array_reverse($list) as $x) {
        $l = count($xs);
        if ($x != "0" || !($l % 4))
            $n = ($x == '0' ? '' : $x) . ($units[($l - 1) % $ul]);
        else
            $n = is_numeric($xs[0][0]) ? $x : '';
        array_unshift($xs, $n);
    }
    return $xs;
}
Copy after login
Copy after login

Related recommendations:

php function to convert numbers into strings of specified length

Detailed introduction to examples of php numbers to Chinese function

Detailed explanation of traps that are more likely to occur with php numbers and strings

The above is the detailed content of PHP number conversion to uppercase function method tutorial. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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