I encountered an interesting small algorithm question during an interview: I required to convert Arabic numerals into Chinese characters for display (including units ). At that time, though implemented, the code was a bit messy. So after going home, I sorted it out again.
I encountered an interesting small algorithm question during an interview: I required to convert Arabic numerals into Chinese characters for display (including units ). At that time, though implemented, the code was a bit messy. So after going home, I sorted it out again.
Directly on the instance, write to hundreds of billions.
/*** @ Author ja * replace the number 1-1 million with Chinese characters, for example: 123-> one hundred and twenty-three * @ param [num] $ num [number] * @ return [string] [string] */function numToWord ($ num) {$ chiNum = array ('0', '1', '2', '3', '4', '5', '6', '7 ', '8', '9'); $ chiUni = array ('', '10', 'bai', 'kil', 'wan', 'yi ', '10', '100', 'kil'); $ chiStr = ''; $ num_str = (string) $ num; $ count = strlen ($ num_str ); $ last_flag = true; // whether the previous one is 0 $ zero_flag = true; // whether it is the first $ temp_num = null; // temporary number $ chiStr = ''; // The splicing result if ($ count = 2) {// two digits $ temp_num = $ num_str [0]; $ chiStr = $ temp_num = 1? $ ChiUni [1]: $ chiNum [$ temp_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 2:
<? Php/*** convert numbers to Chinese characters * @ param string | integer | float $ num target number * @ param integer $ mode [true: Amount (default), false: ordinary number representation] * @ param boolean $ Use lowercase sim (default) * @ return string */function number2chinese ($ num, $ mode = true, $ sim = true) {if (! Is_numeric ($ num) return 'contains non-decimal characters! '; $ Char = $ sim? Array ('0', '1', '2', '3', '4', '5', '6', '7', '8 ', '9'): array ('0', 'yi', 'er', 'san', 'siz', 'wu', 'lu', 'region ', 'authorization', 'authorization'); $ unit = $ sim? Array ('', '10', '100', '100','', '100', '000000'): array ('', 'pick', 'handler', 'handler', ', 'handler', 'handler'); $ retval = $ mode? 'Meta': 'point'; // if (strpos ($ num ,'. ') {list ($ num, $ dec) = explode ('. ', $ num); $ dec = strval (round ($ dec, 2); if ($ mode) {$ retval. = "{$ char [$ dec ['0']} {$ 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 = '2017. 789 '; echo $ num ,'
'; // The Chinese character of a common number indicates echo 'normal:', number2chinese ($ num, false), ''; echo'
'; // The amount of Chinese characters indicates echo' amount (simplified): ', number2chinese ($ num, true), ''; echo'
'; Echo' amount (traditional): ', number2chinese ($ num, true, false );
The above is all the content of this article. I hope you will like it.