PHP functions to convert numbers into RMB capitals share _php tutorial

Source: Internet
Author: User
Tags rtrim
Financial people have encountered such a problem, such as 13,126.8 Yuan, in the invoice or other bookkeeping business, generally use uppercase to spell "one Wan bai Lu Yuan ba corner of the whole".

This is not, also affixed to my original in the network of learning a PHP function, for everyone to refer to it.
Copy CodeThe code is as follows:
/**
* RMB lowercase to uppercase
*
* @param string $number value
* @param string $int _unit currency unit, default "meta", some demand may be "circle"
* @param bool $is _round whether decimals are rounded
* @param bool $is _extra_zero whether the integer part ends with 0, the number of decimal places is appended with 0, for example 1960.30
* @return String
*/
function Rmb_format ($money = 0, $int _unit = ' meta ', $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 more than 2 digits after the decimal point, do not round the direct intercept, otherwise it will be processed
$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 ' 0 ';
}

Defined
$chs = Array (' 0 ', ' one ', ' II ', ' three ', ' establishments ', ' Wu ', ' Lu ', ' qi ', ' ba ', ' JIU ');
$uni = Array (' ', ' pick ', ' bai ', ' thousand ');
$dec _uni = Array (' Angle ', ' min ');
$exp = Array (' ', ' million ');
$res = ";

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 words for a period of conversion, I have been reducing
for ($j = 0; $j < 4 && $i >= 0; $j + +, $i-) {
$u = $int {$i} > 0? $uni [$j]: '; Add units after a number other than 0
$str = $chs [$int {$i}]. $u. $STR;
}
$str = RTrim ($str, ' 0 '); Remove the end of the 0
$str = Preg_replace ("/0+/", "0", $str); Replace multiple consecutive 0
if (! isset ($exp [$k])) {
$exp [$k] = $exp [$k-2]. ' Billion '; Building units
}
$u 2 = $str! = "? $exp [$k]: ';
$res = $str. $u 2. $res;
}

If the fractional part is 00 after processing, you need to handle the
$dec = RTrim ($dec, ' 0 ');
Var_dump ($DEC);
To find the decimal part from left to right
if (! empty ($dec)) {
$res. = $int _unit;

If you want to append 0 to the integer part with a number ending in 0, some systems have this requirement
if ($is _extra_zero) {
if (substr ($int,-1) = = = = ' 0 ') {
$res. = ' 0 ';
}
}

for ($i = 0, $cnt = strlen ($DEC); $i < $cnt; $i + +) {
$u = $dec {$i} > 0? $dec _uni [$i]: '; Add units after a number other than 0
$res. = $chs [$dec {$i}]. $u;
if ($cnt = = 1)
$res. = ' whole ';
}

$res = RTrim ($res, ' 0 '); Remove the end of the 0
$res = Preg_replace ("/0+/", "0", $res); Replace multiple consecutive 0
} else {
$res. = $int _unit. ' Whole ';
}
return $res;
}

It's easy to use.
Copy the Code code as follows:
$yuan = 13598.3;
$ret =rmb_format ($yuan);

http://www.bkjia.com/PHPjc/824745.html www.bkjia.com true http://www.bkjia.com/PHPjc/824745.html techarticle financial people have encountered such a problem, such as 13,126.8 Yuan, in the invoice or other bookkeeping business, generally use uppercase to spell "one Wan bai Lu Yuan ba corner of the whole ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.