PHP code to convert numbers into Chinese characters, PHP digital Kanji Code _php Tutorial

Source: Internet
Author: User

PHP converts numbers into kanji code, PHP digital Kanji Code


Directly on the example, written to hundreds of billions.

/*** @author ja Ode * Converts the number 1.1 billion to a kanji representation, such as:123-> 123 * @param [num] $num [digital]* @return [string] [String]*/function Numtoword ($n UM) {$chiNum = array (' 0 ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six ', ' seven ', ' eight ', ' nine '); $chiUni = Array (' ', ' ten ', ' hundred ', ' thousand ', ' million ', ' billion ', ' ten ', ' hundred ') , ' thousand '); $chiStr = '; $num _str = (string) $num; $count = strlen ($num _str); $last _flag = true; Whether the previous is 0$zero_flag = true; Whether the first $temp_num = null; Temporary number $chistr = ';//concatenation result if ($count = = 2) {//two digits $temp_num = $num _str[0]; $chiStr = $temp _num = = 1? $chiUni [1]: $chiNum [$t Emp_num]. $chiUni [1]; $temp _num = $num _str[1]; $chiStr. = $temp _num = = 0? ": $chiNum [$temp _num];  }else if ($count > 2) {$index = 0;for ($i = $count-1; $i >= 0; $i-) {$temp _num = $num _str[$i];if ($temp _num = = 0) {if (! $zero _flag &&! $last _flag) {$chiStr = $chiNum [$temp _num]. $chiStr; $last _flag = true;}} else{$chiStr = $chiNum [$temp _num]. $chiUni [$index%9]. $chiStr; $zero _flag = false; $last _flag = false;} $index + +;}} else{$chiStr = $chiNum [$num _str[0];} return $CHISTR;} $num = 150;echo Numtoword ($num); 

Method Two:

<?php/*** number converted to Chinese * @param string|integer|float $num target number * @param integer $mode mode [true: Amount (default), false: Normal number indicates]* @para M boolean $sim use lowercase (default) * @return string*/function Number2chinese ($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; }//Instance call ===================================================== $num = ' 0123648867.789 '; Echo $num, '
'; The ordinary number of Chinese characters means echo ' normal: ', Number2chinese ($num, false), '; Echo '
'; Amount Kanji denotes echo ' Amount (Simplified): ', Number2chinese ($num, true), '; Echo '
' Echo ' Amount (Traditional): ', Number2chinese ($num, True,false);

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/1035661.html www.bkjia.com true http://www.bkjia.com/PHPjc/1035661.html techarticle PHP to convert numbers into Chinese characters code, PHP digital Chinese code directly on the example, written to hundreds of billions of. /*** @author ja Ode * Converts the number 1.1 billion into Chinese characters, such as: 123-120 ...

  • 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.