PHP encapsulated _php Example of RMB value to Chinese capital class

Source: Internet
Author: User
Tags explode numeric lowercase numeric value rtrim strlen

PHP-encapsulated renminbi value to Chinese capital class

Class num2rmbclass{/** * RMB value to Chinese capital * @author Sunscheung * @time 2015.11.11 * @param string $number numeric value defaults to 0 * @param string $int _unit currency unit, the default "yuan", some requirements may be "round" * @param bool $is _round Whether decimal is rounded * @param bool $is _extra_zer o to the integer portion ends with 0, decimal number of the existence of additional 0, such as 1960.30, * Some system requirements output "a nine hundred land to pick up the 0-point", in fact, "a nine-to-three-point" is also the * @param bool $dec _to_int whether to let decimal  The part carries to the digit, if carries, the digit adds 1, the decimal number is 0, * @return string/public static function num2rmb ($number = 0, $int _unit = ' Yuan ', $is _round
    = TRUE, $is _extra_zero = False, $dec _to_int = False) {//The number is divided into two segments $parts = explode ('. ', $number, 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, not rounded directly cut, otherwise processing $dec _len = strlen ($DEC); if (Isset ($parts [1]) && $dec _len > 2) {if ($is _round) {if (round Floatval ("0.").
          $DEC), 2 = = 1 && $dec _to_int) {//decimal carry to Single-digit $int = Empty ($int) 1:strval ($parts [0]+1);
$dec = 0;        }elseif (Round (floatval) ("0.").
        $DEC) (2) = = 1) {//Decimal not carry to single-digit $dec = "99"; }else{$dec = substr (STRRCHR (Strval round (Floatval ("0.").
          $DEC), 2)), '. '), 1;
        echo $dec;d IE (' boss ');
      }}else{$dec = 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 ';
    }//Definition $chs = Array (' 0 ', ' one ', ' II ', ' three ', ' Restaurant ', ' Wu ', ' Lu ', ' qi ', ' ba ', ' JIU ');
    $uni = Array (', ' Pick ', ' bai ', ' thousand ');
    $dec _uni = Array (' Angle ', ' min ');
    $exp = Array (', ' million ');

    $res = ';
      The integer portion 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 section of the transformation, I have been in the minus 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). "
      <br> "; $str = RTrim ($str, ' 0 ');//Remove the end of the 0 $str = preg_replace ("/0+/", "0", $str); Replace multiple contiguous 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 processed after 00, the $DEC = RTrim ($dec, ' 0 ') should be processed;
      The decimal part is found 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 a unit $res after a number other than 0. = $chs [$dec {$i}].
      $u; $res = RTrim ($res, ' 0 ');//Remove the end of 0 $res = preg_replace ("/0+/", "0", $res); Replaces multiple consecutive 0} else {$res. = $int _unit.
    ' Whole '; Return $number < 0?
  "(negative)". $res: $res;

 }

}

How to use

$a = new Num2rmbclass;
Echo (NUM2RMBCLASS::NUM2RMB (' 1600020039.9989 ', ' Circle ', False,false,false));

And we'll share a simple one.

Function CNY ($ns) 
{
static $cnums = Array ("0", "one", "II", "San", "Restaurant", "WU", "Lu", "Qi", "ba", "Nine"), 
$cnyunits = Array ("Round "," angle "," cent "), 
$grees = Array (" Pick up "," Bai "," Qian "," Million "," Pick Up "," Bai "," Qian "," billion "); 
List ($ns 1, $ns 2) = Explode (".", $ns, 2); 
$ns 2 = array_filter (Array ($ns 2[1], $ns 2[0])); 
$ret = Array_merge ($ns 2,array (Implode ("", _cny_map_unit (Str_split ($ns 1), $grees)); 
$ret = Implode ("", Array_reverse (_cny_map_unit ($ret, $cnyunits)); 
Return Str_replace (Array_keys ($cnums), $cnums, $ret);

function _cny_map_unit ($list, $units) 
{ 
$ul = count ($units); 
$xs = Array (); 
foreach (Array_reverse ($list) as $x) 
{ 
$l = count ($xs); 
if ($x!= "0" | |! ( $l%4)) 
{
$n = ($x = = ' 0 '? ': $x). ( $units [($l-1)% $ul]); 
}
else
{
$n =is_numeric ($xs [0][0])? $x: '; 
}
Array_unshift ($xs, $n); 
} 
return $xs; 
}

$value = ' 23058.04 ';
Print CNY ($value); Echo '
;

Output: Wan 0 Wu ba round corner

Another net friend did a good job.

/** * Numeric amount converted to Chinese capital amount function *string Int $num lowercase or lowercase strings to be converted *return uppercase letters * Decimal places are two-bit **/function NUM_TO_RMB ($num) {$c 1 = "0"
    Restaurant Woolu Qi Ba Nine ";
    $c 2 = "cent of the points of the hundred million thousand thousand million"; 
    Accurate to the back of the point is not, so leave only two decimal places $num = Round ($num, 2);
    Converts a number to an integer $num = $num * 100;
    if (strlen ($num) > a) {return "The amount is too large, please check";
    } $i = 0;
    $c = "";
        while (1) {if ($i = = 0) {//get last digit $n = substr ($num, strlen ($num)-1, 1);
        else {$n = $num% 10;
        ///each time the last number is translated into Chinese $p 1 = substr ($c 1, 3 * $n, 3);
        $p 2 = substr ($c 2, 3 * $i, 3); if ($n!= ' 0 ' | | ($n = = ' 0 ' && ($p 2 = ' billion ' | | $p 2 = ' million ' | | $p 2 = = ')])
        {$c = $p 1. $p 2. $c;
        else {$c = $p 1. $c;
        } $i = $i + 1;
        Remove the number the last one $num = $num/10;
        $num = (int) $num;
        End Loop if ($num = = 0) {break;
    }} $j = 0;
    $slen = strlen ($c); WhilE ($j < $slen) {//utf8 A character of quite 3 characters $m = substr ($c, $j, 6); Deal with many 0 of the number of cases, each cycle to remove a Chinese character "0" if ($m = = ' 0 ' | | | $m = = ' 0 ' | | | $m = = ' 0 ' | | $m = = ' 00 ') {$left = substr
            ($c, 0, $j);
            $right = substr ($c, $j + 3); $c = $left.
            $right;
            $j = $j-3;
        $slen = $slen-3;
    } $j = $j + 3;
    //This is to remove the last "0" word in the 23.0 (substr ($c, strlen ($c)-3, 3) = = ' 0 ') {$c = substr ($c, 0, strlen ($c)-3);
    ///Add the processed Chinese character "whole" if (empty ($c)) {return "0 yuan"; }else{return $c.
    "Whole"; } Echo NUM_TO_RMB (23000000.00); Three thousand million Yuan 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.