Home  >  Article  >  Backend Development  >  How to convert numbers to uppercase in php

How to convert numbers to uppercase in php

藏色散人
藏色散人Original
2021-09-12 09:47:062833browse

php method to convert numbers to uppercase: 1. Create a PHP sample file; 2. Define a "function cny($ns) {...}" method; 3. Use cny and "_cny_map_unit" method to convert.

How to convert numbers to uppercase in php

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

php How to convert numbers to uppercase?

When we write a payment bill, we need to fill in the numerical amount in capital letters. To be honest, we usually don’t. I really couldn't write it down, so in order to avoid embarrassing situations in the future, I even practiced 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;
}

Related introduction:

is_numeric() function is used to detect whether the variable is a number or a number string.

PHP version requirements: PHP 4, PHP 5, PHP 7

Syntax

bool is_numeric ( mixed $var )

Parameter description:

$var: Variable to be detected.

Return value

If the specified variable is a number or numeric string, it returns TRUE, otherwise it returns FALSE. Note that floating point type returns 1, which is TRUE.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert numbers to uppercase 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