PHP RMB lowercase to uppercase function, unlimited length, accurate to cent (recommended)

Source: Internet
Author: User
Tags rtrim
This article introduces a PHP implementation of a small capitalization of the function, unlimited digital length, can be accurate to the points. If you need a friend, refer to it.

When you print an invoice or display a bill, you often need to capitalize the RMB amount from lowercase.

The following is an improved function of the small capitalization of the renminbi, the function is as follows: 1. Support astronomical, the whole number of the theory can be infinitely long; 2. Support decimal, for currency, generally accurate to decimal two digits, you can set whether the decimal place is rounded; 3. Support custom currency units, some systems require capital is "round", some requirements are "meta", can be customized; 4. Support integer 0 to end with a decimal number to customize the end of the "0", for example, some system requirements 1960.30 such a number to the capital is "a thousand and nine hundred Yuan three corner", and some systems require "a thousand and a nine hundred Yuan," the three corners ", these two cases according to the" correctly fill in the basic provisions "Are correct and can now be customized.

Instance code:

 
     2) {$dec = $is _round substr (STRRCHR (Strval (Round ("0.").        $DEC), 2)), '. '), 1): substr ($parts [1], 0, 2);        }//When number is 0.001, the amount after the decimal point is $0 if (empty ($int) && empty ($dec)) {return ' 0 ';        }//Definition $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 = ";            The integer part is 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 reduced for ($j = 0; $j < 4 && $i >= 0; $j + +, $i-) { $u = $int {$i} > 0? $uni [$j]: '; Add unit $str = $chs [$int {$i}] after a number other than 0. $u.            $STR; }//echo $str. "|". ($k-2). "
"; $str = RTrim ($str, ' 0 ');//Remove 0 $str at the end = Preg_replace ("/0+/", "0", $str); Replace multiple consecutive 0 if (!isset ($exp [$k])) {$exp [$k] = $exp [$k-2]. ' Billion '; Build Unit} $u 2 = $str! = "? $exp [$k]: '; $res = $str. $u 2. $res; }//If the fractional part is 00 after processing, the next $dec = RTrim ($dec, ' 0 ') needs to be processed; The decimal part is left-to-right for if (!empty ($dec)) {$res. = $int _unit; Whether 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} & Gt 0? $dec _uni[$i]: "; A number other than 0 adds the unit $res. = $chs [$dec {$i}]. $u; } $res = RTrim ($res, ' 0 ');//Remove the end of 0 $res = preg_replace ("/0+/", "0", $res); Replace multiple consecutive 0}else {$res. = $int _unit. ' Whole '; } return $res; } echo "
";    $number = "1000000000000000012345678900.501";    echo $number. ":". NUM2RMB ($number);    echo "\ n";    $number = "1960.30";    echo $number. ":". NUM2RMB ($number);    echo "\ n";    $number = "1960.30";    echo $number. ":". NUM2RMB ($number, "Round", true, true);    echo "\ n";    $number = "123456789.005";    echo $number. ":". NUM2RMB ($number);    echo "\ n";    $number = "123456789.005";    echo $number. ":". NUM2RMB ($number, "Yuan", false);    echo "\ n";    $number = "10000000000000000060009.101";    echo $number. ":". NUM2RMB ($number);    echo "\ n";    $number = "1680.32";    echo $number. ":". NUM2RMB ($number);? >

Output: 1000000000000000012345678900.501: One billion billion hundred hundred million in the Wu Bai Lu pick qi Wan jiu bai yuan Wu angle 1960.30: A thousand nine Bai Lu Yuan San jiao 1960.30: A thousand nine Bai Lu gather round 0 three corner 123456 789.005: One Billion II San Bai restaurant pick Wu Wanlu thousand qi bai ba pick Shangyuan 0 A cent 123456789.005: Yi billion II three Bai restaurant pick up Wu Wanlu thousand qi bai ba pick Shangyuan 10000000000000000060009.101: Hundred trillion billion zero Lu Wanzu Shangyuan one corner 1680.32 : Yi Lu bai ba Yuan San Jiao three points

  • Related Article

    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.