How to convert lowercase RMB to uppercase in php

藏色散人
Release: 2023-03-08 18:56:01
Original
2539 people have browsed it

php Method to convert RMB from lowercase to uppercase: First create a PHP sample file; then pass "function num2rmb($number = 0, $int_unit = '', $is_round=FALSE...){... }" method can be used to convert RMB from lowercase to uppercase.

How to convert lowercase RMB to uppercase in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

php RMB lowercase to uppercase function, not Limited length, accurate to minutes

When printing invoices or displaying bills, we often need to convert the amount of RMB from lowercase to uppercase. The following is a function I improved, which supports the following features:

  • Supports astronomical numbers, the integer digits can theoretically be infinitely long;

  • Supports decimals, for currency, it is generally accurate to two decimal places , you can set whether the decimal places are rounded;

  • Supports custom currency units. Some systems require the capital to be "round", and some require "yuan", which can be customized;

  • Supports integers ending in 0 and customizing numbers containing decimals with "zero" at the end. For example, some systems require that a number like 1960.30 be converted to uppercase to be "One Thousand Nine Hundreds Lu Shi Yuan Three" "Jiao", while some systems require "One thousand, nine hundred, ten yuan and three cents". In both cases, they are correct according to the "basic regulations for correctly filling in bills and settlement vouchers", and can now be customized.

/** 
 * 人民币小写转大写 
 * 
 * @param string $number 数值 
 * @param string $int_unit 币种单位,默认"元",有的需求可能为"圆" 
 * @param bool $is_round 是否对小数进行四舍五入 
 * @param bool $is_extra_zero 是否对整数部分以0结尾,小数存在的数字附加0,比如1960.30, 
 *             有的系统要求输出"壹仟玖佰陆拾元零叁角",实际上"壹仟玖佰陆拾元叁角"也是对的 
 * @return string 
 */ 
function num2rmb($number = 0, $int_unit = '', $is_round = FALSE, $is_extra_zero = FALSE , $rmb = FALSE) 
{ 
    // 将数字切分成两段 
    $parts = explode('.', $number, 2); 
    $int = isset($parts[0]) ? strval($parts[0]) : '0'; 
    $dec = isset($parts[1]) ? strval($parts[1]) : ''; 
 
    // 如果小数点后多于2位,不四舍五入就直接截,否则就处理 
    $dec_len = strlen($dec); 
    if (isset($parts[1]) && $dec_len > 2) 
    { 
        $dec = $is_round 
                ? substr(strrchr(strval(round(floatval("0.".$dec), 2)), '.'), 1) 
                : substr($parts[1], 0, 2); 
    } 
 
    // 当number为0.001时,小数点后的金额为0元 
    if(empty($int) && empty($dec)) 
    { 
        return '零'; 
    } 
 
    // 定义 
    if($rmb){
        $chs = array('0','壹','贰','叁','肆','伍','陆','柒','捌','玖'); 
        $uni = array('','拾','佰','仟'); 
        $dec_uni = array('角', '分'); 
        $exp = array('', '万'); 
    }else {
        $chs = array('0','一','二','三','四','五','六','七','八','九'); 
        $uni = array('','十','百','千'); 
        $dec_uni = array('角', '分'); 
        $exp = array('', '万'); 
    }
    
 
   
 
 
    $res = ''; 
 
    // 整数部分从右向左找 
    for($i = strlen($int) - 1, $k = 0; $i >= 0; $k++) 
    { 
        $str = ''; 
        // 按照中文读写习惯,每4个字为一段进行转化,i一直在减 
        for($j = 0; $j < 4 && $i >= 0; $j++, $i--) 
        { 
            $u = $int{$i} > 0 ? $uni[$j] : &#39;&#39;; // 非0的数字后面添加单位 
            $str = $chs[$int{$i}] . $u . $str; 
        } 
        //echo $str."|".($k - 2)."<br>"; 
        $str = rtrim($str, &#39;0&#39;);// 去掉末尾的0 
        $str = preg_replace("/0+/", "零", $str); // 替换多个连续的0 
        if(!isset($exp[$k])) 
        { 
            $exp[$k] = $exp[$k - 2] . &#39;亿&#39;; // 构建单位 
        } 
        $u2 = $str != &#39;&#39; ? $exp[$k] : &#39;&#39;; 
        $res = $str . $u2 . $res; 
    } 
 
    // 如果小数部分处理完之后是00,需要处理下 
    $dec = rtrim($dec, &#39;0&#39;); 
 
    // 小数部分从左向右找 
    if(!empty($dec)) 
    { 
        $res .= $int_unit; 
 
        // 是否要在整数部分以0结尾的数字后附加0,有的系统有这要求 
        if ($is_extra_zero) 
        { 
            if (substr($int, -1) === &#39;0&#39;) 
            { 
                $res.= &#39;零&#39;; 
            } 
        } 
 
        for($i = 0, $cnt = strlen($dec); $i < $cnt; $i++) 
        { 
            $u = $dec{$i} > 0 ? $dec_uni[$i] : &#39;&#39;; // 非0的数字后面添加单位 
            $res .= $chs[$dec{$i}] . $u; 
        } 
        $res = rtrim($res, &#39;0&#39;);// 去掉末尾的0 
        $res = preg_replace("/0+/", "零", $res); // 替换多个连续的0 
    } 
    else 
    { 
        if($rmb){
            $res .= $int_unit . &#39;整&#39;; 
        }
        
    } 
    return $res; 
}
Copy after login

Output result:

1000000000000000012345678900.501:壹仟亿亿亿零壹佰贰拾叁亿肆仟伍佰陆拾柒万捌仟玖佰元伍角 
1960.30:壹仟玖佰陆拾元叁角 
1960.30:壹仟玖佰陆拾圆零叁角 
123456789.005:壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖元零壹分 
123456789.005:壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖元整 
10000000000000000060009.101:壹佰万亿亿零陆万零玖元壹角 
1680.32:壹仟陆佰捌拾元叁角贰分
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert lowercase RMB to uppercase 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!