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
Jul 13, 2016 am 10:26 AM
php
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);
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

Hot Article
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
