Home > Backend Development > PHP Tutorial > PHP function to convert numbers into RMB capital letters_PHP tutorial

PHP function to convert numbers into RMB capital letters_PHP tutorial

WBOY
Release: 2016-07-13 10:26:17
Original
1108 people have browsed it

People who work in finance have encountered such a problem. For example, 13,126.8 yuan. In invoices or other accounting services, it is generally necessary to use capital letters to spell it as "One Thousand Thirty Thousand One Hundred Twenty Six Lu Yuan Eighty Points" .

No, I’ll also post a php function that I used to work on on xuedriving.com for your reference.

Copy code The code is as follows:

/**
* Convert RMB from lower case to upper case
*
* @param string $number value
* @param string $int_unit currency unit, the default is "yuan", some requirements may be "yuan"
* @param bool $is_round whether to round decimals
* @param bool $is_extra_zero Whether to end the integer part with 0 and add 0 to the number where the decimal exists, such as 1960.30
* @return string
​*/
function rmb_format($money = 0, $int_unit = '元', $is_round = true, $is_extra_zero = false) {
//Cut the number into two segments
$parts = explode ( '.', $money, 2 );
$int = isset ( $parts [0] ) ? strval ( $parts [0] ) : '0';
$dec = isset ( $parts [1] ) ? strval ( $parts [1] ) : '';

// If there are more than 2 digits after the decimal point, just truncate without rounding, otherwise process
$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 );
}

// When number is 0.001, the amount after the decimal point is 0 yuan
If (empty ( $int ) && empty ( $dec )) {
          return 'zero';
}

// Definition
$chs = array ('0', '一', '二', '三', '四', '五', 'LU', '旒', '捌', '玖' );
$uni = array ('', '十', 'hundred', 'qian' );
$dec_uni = array ('angle', 'cent' );
$exp = array ('', '万' );
$res = '';

// Search the integer part from right to left
for($i = strlen ( $int ) - 1, $k = 0; $i >= 0; $k ++) {
          $str = '';
                         // According to Chinese reading and writing habits, every 4 characters are converted into a paragraph, and the i is constantly decreasing
for($j = 0; $j < 4 && $i >= 0; $j ++, $i --) {
                  $u = $int {$i} > 0 ? $uni [$j] : ''; // Add units after non-0 numbers
$str = $chs [$int {$i}] . $u . $str;
        }
           $str = rtrim ( $str, '0' ); // Remove the trailing 0
          $str = preg_replace ( "/0+/", "zero", $str ); // Replace multiple consecutive 0s
If (! isset ( $exp [$k] )) {
                $exp [$k] = $exp [$k - 2] . 'Billion'; // Construction unit
        }
           $u2 = $str != '' ? $exp [$k] : '';
$res = $str . $u2 . $res;
}

// If the decimal part is 00 after processing, it needs to be processed
$dec = rtrim ( $dec, '0' );
var_dump ( $dec );
// Find the decimal part from left to right
If (! empty ( $dec )) {
          $res .= $int_unit;

// Whether to add 0 at the number of numbers at the integer part at the end of 0. Some systems have this requirement
If ($is_extra_zero) {
If (substr ( $int, - 1 ) === '0') {
                    $res .= 'zero';
            }
        }

for($i = 0, $cnt = strlen ($dec); $i < $cnt; $i ++) {                    $u = $dec {$i} > 0 ? $dec_uni [$i] : ''; // Add units after non-0 numbers
                $res .= $chs [$dec {$i}] . $u;
                 if ($cnt == 1)
$ Res. = 'Tong';
       }

           $res = rtrim ( $res, '0' ); // Remove the trailing 0
          $res = preg_replace ( "/0+/", "zero", $res ); // Replace multiple consecutive 0s
} else {
           $res .= $int_unit . 'whole';
}
Return $res;
}

It’s very easy to use

Copy code The code is as follows:

$yuan=13598.3;
$ret=rmb_format($yuan);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824745.htmlTechArticlePeople who work in finance have encountered such a problem, such as 13,126.8 yuan, in invoices or other accounting services. Generally, it should be spelled in capital letters as "One Thousand Thirty One Hundred Twenty Six Lu Yuan Eighth Points...
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