PHP Arabic numerals to Chinese RMB capital _php tips

Source: Internet
Author: User
Tags lowercase php programming

This example for you to share the PHP Arabic numerals to Chinese capital of the implementation code, for your reference, the specific code as follows

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

/** * 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

Code 2:php Arabic numerals to Chinese capital amount

Arabic numeral 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 ', ' land ', ' qi ', ' ba ', ' JIU '); $unit = $sim?
  Array (', ' ten ', ' hundred ', ' thousand ', ', ' million ', ' billion ', ' trillion ': Array (', ' Pick ', ' bai ', ' thousand ', ', ' million ', ' billion ', ' trillion '); $retval = $mode?
  ' Yuan ': ' Point '; Decimal Parts 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> ");

Hopefully this article will help you learn about PHP programming.

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.