This article mainly introduces how PHP converts numbers into Chinese characters, and is interested in the reference of friends, hoping to help you.
In an interview encounter an interesting small algorithm problem: the need to convert Arabic numerals to Chinese characters display (including units). It was realized, but the code was a little messy. So when I got home, I tidied up again.
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, ' <br> '; The ordinary numbers of Chinese characters indicate echo ' normal: ', Number2chinese ($num, false), '; Echo ' <br> '; Amount Kanji denotes echo ' Amount (Simplified): ', Number2chinese ($num, true), '; Echo ' <br> '; Echo ' Amount (Traditional): ', Number2chinese ($num, True, FALSE);
Summary: the above is the entire content of this article, I hope to be able to help you learn.
Related recommendations:
Basic knowledge and application of PHP design pattern
The definition and generation method of PHP barcode
Nine ways to cache PHP