PHP converts a numeric amount into a Chinese capital amount function

Source: Internet
Author: User
This article mainly introduces the PHP to convert the digital amount to the Chinese capital amount function, the interest friend's reference, hoped to be helpful to everybody.

Code 1:php Arabic to Chinese yuan capitalized, with detailed comments

/*** A function that converts a numeric amount to a Chinese capital amount *string Int $num the lowercase or lowercase string to convert *return uppercase * Decimal places are two-bit **/function NUM_TO_RMB ($num) {$c 1 = "0 One and three Woolu seven BA Nine"    ;    $c 2 = "split-angle element pick Bai thousand thousand million";     Accurate to the point after the not, so only two decimal digits $num = Round ($num, 2);    Converts a number to an integer $num = $num * 100;    if (strlen ($num) >) {return "amount is too large, please check";    } $i = 0;    $c = "";        while (1) {if ($i = = 0) {//Gets the last digit $n = substr ($num, strlen ($num)-1, 1);        } else {$n = $num% 10;        }//each time the last digit is converted to 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 = = ' Yuan ')))        {$c = $p 1. $p 2. $c;        } else {$c = $p 1. $c;        } $i = $i + 1;        Minus the last digit of the number $num = $num/10;        $num = (int) $num;        End Loop if ($num = = 0) {break;    }} $j = 0;    $slen = strlen ($c);  while ($j < $slen) {//utf8 A kanji of 3 characters      $m = substr ($c, $j, 6); Deal with a lot of 0 of the number of cases, each cycle to remove a kanji "0" if ($m = = ' 0 Yuan ' | | $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 similar 23.0 in the last "0" word if (substr ($c, strlen ($c)-3, 3) = = ' 0 ') {$c = substr ($c, 0, strlen ($c)-3);    }//The kanji will be processed with an "integer" If (Empty ($c)) {return "0 yuan whole"; }else{return $c.    "Whole"; }}echo NUM_TO_RMB (23000000.00); Three hundred thousand million whole

Code 2:php Arabic to Chinese capitalization amount

Arabic numerals to Chinese capital amount function Numtocnmoney ($num, $mode = True, $sim = True) {if (!is_numeric ($num)) return ' contains non-numeric non-decimal characters!  '; $char = $sim?  Array (' 0 ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six ', ' seven ', ' eight ', ' nine '): Array (' 0 ', ' one ', ' II ', ' three ', ' the ', ' ' Wu ', ' Lu ', ' qi ', ' ba ', ' JIU '); $unit = $sim?  Array (' ', ' ten ', ' hundred ', ' thousand ', ' ', ' million ', ' billion ', ' Mega '): Array (', ' Pick ', ' bai ', ' thousand ', ' ', ', ' Yi ', ' trillion '); $retval = $mode?  ' Yuan ': ' Point '; Decimal part if (Strpos ($num, '. '))    {List ($num, $dec) = Explode ('. ', $num);    $dec = Strval (Round ($dec, 2));    if ($mode) {$retval. = "{$char [$dec [' 0 ']} corner {$char [$dec [' 1 ']}";      }else{for ($i = 0, $c = strlen ($dec), $i < $c, $i + +) {$retval. = $char [$dec [$i]]; }}}//integer part $str = $mode?  Strrev (Intval ($num)): Strrev ($num);    for ($i = 0, $c = strlen ($str), $i < $c, $i + +) {$out [$i] = $char [$STR [$i]];        if ($mode) {$out [$i]. = $STR [$i]! = ' 0 '? $unit [$i%4]: ';      if ($i >1 and $str [$i]+ $str [$i-1] = = 0) {$out [$i] = ';      if ($i%4 = = 0) {$out [$i]. = $unit [4+floor ($i/4)];  }  }} $retval = Join (', Array_reverse ($out)).  $retval;  return $retval;} Echo (Numtocnmoney (2.55). " <br> "), Echo (Numtocnmoney (2.55,1,0)." <br> "), Echo (Numtocnmoney (7965)." <br> "), Echo (Numtocnmoney (7965,1,0)." <br> "), Echo (Numtocnmoney (155555555.68)." <br> "), Echo (Numtocnmoney (155555555.68,1,0)." <br> "), Echo (Numtocnmoney (0.8888888)." <br> "), Echo (Numtocnmoney (0.8888888,1,0)." <br> "), Echo (Numtocnmoney (99999999999)." <br> "), Echo (Numtocnmoney (99999999999,1,0)." <br> ");

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.