PHP implementation to capitalize the RMB amount

Source: Internet
Author: User
Tags foreach array return

The occasional encounter in the work of the need to convert the amount of renminbi, that is, Arabic numerals into uppercase characters, the following is the author's practice summed up the method, and hereby record to prevent the memo.

Class num2cny{static $basical =array (0=> ' 0 ', ' one ', ' II ', ' San ', ' Restaurant ', ' Wu ', ' Lu ', ' qi ', ' ba ', ' JIU ');
  Static $advanced =array (1=> ' pick ', ' bai ', ' thousand ');
    public static function Parsenumber ($number) {$number =trim ($number);
    if (!is_numeric ($number) intval ($number) >999999999999) return ' error ';
    if ($number ==0) return ' 0 '; if (Strpos ($number, '. '))
      {$number =round ($number, 2);
      $data =explode ('. ', $number);
      $data [0]=self::int ($data [0]);
      $data [1]=self::d EC ($data [1]);
    return $data [0]. $data [1]; }else{return Self::int ($number). '
    Whole ';
    public static function int ($number) {$arr =array_reverse (Str_split ($number));
    $data = ';
    $zero =false;
    $zero _num=0;
      foreach ($arr as $k => $v) {$_chinese= ';
      $zero = ($v ==0) True:false;
      $x = $k% 4;
      if ($x && $zero && $zero _num>1) continue;
          Switch ($x) {case 0:if ($zero) {$zero _num=0; }else{$_chinese=self:: $basical [$V];
          $zero _num=1;
          if ($k ==8) {$_chinese.= ' billion ';
          }elseif ($k ==4) {$_chinese.= ' million ';  
        } break;
              Default:if ($zero) {if ($zero _num==1) {$_chinese=self:: $basical [$v];
            $zero _num++;
            }else{$_chinese=self:: $basical [$v];
          $_chinese.=self:: $advanced [$x];
    }} $data =$_chinese. $data;
  return $data. ' Yuan ';
    The public static function Dec ($number) {if (strlen ($number) <2) $number. = ' 0 ';
    $arr =array_reverse (Str_split ($number));
    $data = ';
    $zero _num=false;
      foreach ($arr as $k => $v) {$zero = ($v ==0)? True:false;
      $_chinese= ';
          if ($k ==0) {if (! $zero) {$_chinese=self:: $basical [$v];
          $_chinese.= ' min ';
        $zero _num=true;
    }}else{if ($zero) {if ($zero _num) {$_chinese=self:: $basical [$v];      }else{$_chinese=self:: $basical [$v];
        $_chinese.= ' horn ';
    }} $data =$_chinese. $data;
  return $data; }
}

The use process is also simple, as follows:

echo NUM2CNY::P arsenumber (1234567.5);//www.alixixi.com

The result will be output:

I found three thousand million land to pick up seven Yuan Wu Jiao

Another explanation is that if you only need to convert Arabic numerals to uppercase, you can refer to this site article:

PHP converts Arabic numerals into Chinese character functions



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.