This article provides a summary of several php practical functions that convert the amount numbers into Chinese capital, each of which has its own advantages and disadvantages. You can choose based on your project needs.
This article provides a summary of several php practical functions that convert the amount numbers into Chinese capital, each of which has its own advantages and disadvantages. You can choose based on your project needs.
Php converts the amount into uppercase letters
Echo toChineseNumber (1234567890); // The number of tens of millions of records can be retrieved from the database. The function toChineseNumber ($ money) {$ money = round ($ money, 2); $ cnynums = array ("zero", "one", "two", "three", "Si", "Wu", "Lu", "Lu ", "dimensions", "dimensions"); $ cnyunits = array ("circle", "Corner", "Minute"); $ cnygrees = array ("pick", "Hour ", "average", "", "Pick", "average", "minimum", ""); list ($ int, $ dec) = explode (". ", $ money, 2); $ dec = array_filter (array ($ dec [1], $ dec [0]); $ ret = array_merge ($ dec, array (implode ("", cnyMapUnit (str_split ($ Int), $ cnygrees), ""); $ ret = implode ("", array_reverse (cnyMapUnit ($ ret, $ cnyunits ))); return str_replace (array_keys ($ cnynums), $ cnynums, $ ret);} function cnyMapUnit ($ list, $ units) {$ ul = count ($ units ); $ xs = array (); foreach (array_reverse ($ list) as $ x) {$ l = count ($ xs); if ($ x! = "0" |! ($ L % 4) $ n = ($ x = '0 '? '': $ X). ($ units [(L-1) % $ ul]); else $ n = is_numeric ($ xs [0] [0])? $ X: ''; array_unshift ($ xs, $ n);} return $ xs ;}
Code 2:
/*** Function for converting the numeric amount to the Chinese capital amount * String Int $ num the lowercase number to be converted or the lowercase String * return uppercase letter * **/function num_to_ RMB ($ num) {$ c1 = ""; $ c2 = ""; // no more accurate to the end of the minute, so leave only two decimal places $ num = round ($ num, 2); // convert the number to an integer $ num = $ num * 100; if (strlen ($ num)> 10) {return "the amount is too large. Please check" ;}$ I = 0; $ c = ""; while (1) {if ($ I = 0) {// obtain the last digit $ n = substr ($ num, strlen ($ num)-1, 1);} else {$ n = $ num % 10 ;} // convert the last digit to Chinese $ p1 = substr ($ c1, 3 * $ N, 3); $ p2 = substr ($ c2, 3 * $ I, 3); if ($ n! = '0' | ($ n = '0' & ($ p2 = '000000' | $ p2 = '000000' | $ p2 =' yuan '))) {$ c = $ p1. $ p2. $ c;} else {$ c = $ p1. $ c;} $ I = $ I + 1; // remove the last digit from the number $ num = $ num/10; $ num = (int) $ num; // end loop if ($ num = 0) {break ;}}$ j = 0; $ slen = strlen ($ c); while ($ j <$ slen) {// utf8 a Chinese character is equivalent to 3 characters $ m = substr ($ c, $ j, 6); // process many numbers with 0 values, remove a Chinese character "zero" if ($ m = 'zero meta' | $ m = '000000' | $ m = '000000' | $ m = 'zero id ') {$ 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 "zero" word like 23.0 if (substr ($ c, strlen ($ c)-3, 3) = '0 ') {$ c = substr ($ c, 0, strlen ($ c)-3);} // Add the processed Chinese characters to an integer if (empty ($ c )) {return "Zero yuan";} else {return $ c. "whole" ;}} echo num_to_ RMB (23000000.00); // tens of thousands of RMB
Code 3:
<? Php // first paste a number into Chinese, a maximum of 12 digits function convert_2_cn ($ num) {$ convert_cn = array ("zero", "one", "two ", "San", "Si", "Wu", "Lu", "Lu", "Wu", "Wu "); $ repair_number = array '); $ unit_cn = array ("", ""); $ exp_cn = array ("", "", ""); $ max_len = 12; $ len = strlen ($ num); if ($ len> $ max_len) {return 'outnumber ';} $ num = str_pad ($ num, 12, '-', STR_PAD_LEFT); $ exp_num = array (); $ k = 0; for ($ I = 12; $ I> 0; $ I --) {if ($ I % 4 = 0) {$ k ++;} $ exp_num [$ k] [] = substr ($ num, $ I-1, 1) ;}$ str = ''; foreach ($ exp_num as $ key => $ nums) {if (array_sum ($ nums) {$ str = array_shift ($ exp_cn ). $ str;} foreach ($ nums as $ nk => $ nv) {if ($ nv = '-') {continue;} if ($ nk = 0) {$ str = $ convert_cn [$ nv]. $ str;} else {$ str = $ convert_cn [$ nv]. $ unit_cn [$ NK-1]. $ str ;}}$ str = str_replace ($ repair_number, array ('wan', 'yi', '-'), $ str ); $ str = preg_replace ("/-{2 ,}/", "", $ Str); $ str = str_replace (array ('0', '-'), array ('', '0'), $ str ); return $ str;} echo convert_2_cn (1111 ). "\ n"; echo convert_2_cn (111111 ). "\ n"; echo convert_2_cn (111111111111 ). "\ n"; // Add a function cn_2_num ($ str) {$ convert_cn = array ("zero", "one", "two ", "San", "Si", "Wu", "Lu", "shen"); $ skip_words = array ("pick ", "comment", "comment"); $ str = str_replace ($ skip_words, "", $ str); $ len = mb_strlen ($ str, 'utf-8 '); $ num = 0; $ k = ''; for ($ I = 0; $ I <$ len; $ I ++) {$ cn = mb_substr ($ str, $ I, 1, 'utf-8 '); if ($ cn = '') {$ num = $ num + intval ($ k) * 100000000; $ k = '';} elseif ($ cn = 'wan') {$ num = $ num + intval ($ k) * 10000; $ k = '';} else {$ k = $ k. array_search ($ cn, $ convert_cn) ;}} if ($ k) {$ num = $ num + intval ($ k) ;}return $ num ;} echo cn_2_num '). "\ n"; echo cn_2_num '). "\ n";?>
Code 4:
Function convertCurrency (currencyDigits) {// Constants: var MAXIMUM_NUMBER = 99999999999.99; // Predefine the radix characters and currency symbols for output: var CN_ZERO = "zero "; var CN_ONE = ""; var CN_TWO = ""; var CN_THREE = ""; var CN_FOUR = "Si"; var CN_FIVE = "Wu "; var CN_SIX = ""; var CN_SEVEN = "success"; var CN_EIGHT = "success"; var CN_NINE = "success"; var CN_TEN = "pick up "; var CN_HUNDRED = "canonical"; var CN_THOUSAND = "canonical"; var CN_TEN_THOUSAND = ""; var CN_HUNDRED_MILLION = ""; var CN_SYMBOL = "RMB"; var CN_DOLLAR = "RMB"; var CN_TEN_CENT = ""; var CN_CENT = "points"; var CN_INTEGER = "whole"; // Variables: var integral; // Represent integral part of digit number. var decimal; // Represent decimal part of digit number. var outputCharacters; // The output result. var parts; var digits, radices, bigRadices, decimals; var zeroCount; var I, p, d; Var quotient, modulus; // Validate input string: currencyDigits = currencyDigits. toString (); if (currencyDigits = "") {alert ("Empty input! "); Return" ";} if (currencyDigits. match (/[^,. \ d]/)! = Null) {alert ("Invalid characters in the input string! "); Return" ";} if (currencyDigits ). match (/^ (\ d {1, 3} (, \ d {3 })*(. (\ d {3},) * \ d {1, 3 }))?) | (\ D + (. \ d + )?)) $/) = Null) {alert ("Illegal format of digit number! "); Return" ";} // Normalize the format of input digits: currencyDigits = currencyDigits. replace (/,/g, ""); // Remove comma delimiters. currencyDigits = currencyDigits. replace (/^ 0 +/, ""); // Trim zeros at the beginning. // Assert the number is not greater than the maximum number. if (Number (currencyDigits)> MAXIMUM_NUMBER) {alert ("Too large a number to convert! "); Return" ";}// Process the coversion from currency digits to characters: // Separate integral and decimal parts before processing coversion: parts = currencyDigits. split (". "); if (parts. length> 1) {integral = parts [0]; decimal = parts [1]; // Cut down redundant decimal digits that are after the second. decimal = decimal. substr (0, 2);} else {integral = parts [0]; decimal = "";} // Prepare the charact Ers corresponding to the digits: digits = new Array (CN_ZERO, CN_ONE, numbers, CN_THREE, CN_FOUR, CN_FIVE, tags, CN_SEVEN, CN_EIGHT, CN_NINE); radices = new Array ("", CN_TEN, CN_HUNDRED, CN_THOUSAND); bigRadices = new Array ("", percentages, percentages); decimals = new Array (CN_TEN_CENT, CN_CENT); // Start processing: outputCharacters = ""; // Process integral part if it is larger than 0: if (Nu Mber (integral)> 0) {zeroCount = 0; for (I = 0; I <integral. length; I ++) {p = integral. length-I-1; d = integral. substr (I, 1); quotient = p/4; modulus = p % 4; if (d = "0") {zeroCount ++ ;} else {if (zeroCount> 0) {outputCharacters + = digits [0];} zeroCount = 0; outputCharacters + = digits [Number (d)] + radices [modulus];} if (modulus = 0 & zeroCount <4) {outputCharacters + = bigRadices [Quotient] ;}}outputcharacters + = CN_DOLLAR;} // Process decimal part if there is: if (decimal! = "") {For (I = 0; I <decimal. length; I ++) {d = decimal. substr (I, 1); if (d! = "0") {outputCharacters + = digits [Number (d)] + decimals [I] ;}}// Confirm and return the final output string: if (outputCharacters = "") {outputCharacters = CN_ZERO + CN_DOLLAR;} if (decimal = "") {outputCharacters + = CN_INTEGER;} // outputCharacters = CN_SYMBOL + outputCharacters; outputCharacters = outputCharacters; return outputCharacters;} // var stmp = ""; function nst_convert (t) {if (t. value = s Tmp) return; // return var MS = t if it is equal to the previous input. value. replace (/[^ \ d \.] /g ,""). replace (/(\. \ d {2 }). + $/, "$1 "). replace (/^ 0 + ([1-9])/, "$1 "). replace (/^ 0 + $/, "0"); // replace (/[^ \ d \.] /g, "") Remove the numbers and. /replace (/(\. \ d {2 }). + $/, "$1") // match the first character from the start. because no g mark is used for all subsequent characters, // only match the first time and then replace it with the decimal point and the last two digits to confirm that the final format of the value is correct and high. // replace (/^ 0 + ([1-9])/, "$1 ") replace the value starting with multiple zeros with the value after removing zeros as the first digit. // replace (/^ 0 + $/, "0") matches multiple 0 s starting from and ending with 0 as one 0 That is, 0000000 input-> convert to a 0 // The following determines the input is the legal number after filtering // alert (ms); var txt = ms. split (". "); // alert (txt [0]); // If the ms value does not have a decimal point, txt [0] = the value before the decimal point; otherwise, it is equal to ms // regexp: /\ d {4} (, | $)/a set that matches four digits and, or a set of four digits and the end of the character while (/\ d {4 }(, | $ )/. test (txt [0]) // if it is txt [0] = 4123txt [0] = txt [0]. replace (/(\ d) (\ d {3} (, | $)/, "$1, $2"); // txt [0]. replace (/(\ d) (\ d {3} (, | $)/, "$1, $2 ") is to replace txt [0] And then assign it // regexp:/(\ d) (\ d {3} (, | $ )) /set the four digits as the first digit in the two groups, and the last three digits and the rest as each two digits. // And replace it with the first one. Note that the second one is very useful. that is to say, replace 4123 with 4,123 first. // because this expression adopts greedy match by default, the value is matched forward and then matched and replaced by loop to divide // 12345678 into what you want. 123,456, 78 the usage of (, | $) is very typical, because it omitted the problem t during the second match. value = stmp = txt [0] + (txt. length> 1? ". "+ Txt [1]:" "); // The final value is assigned to the input box. // Add the decimal point to the final number. Otherwise, the replaced txt [0] bbb is displayed. value = convertCurrency (ms-0); // converts ms to numbers and sends them to number2num1 for conversion}
The above is all the content of this article. I hope you will like it.