There was nothing to worry about, and I wrote an example of converting numbers into RMB capital. The main difficulty is that "0" is in different locations and the processing method is different.
Here, we consider a general number with an integer of up to 12 digits and two decimal places. That is, the minimum value is 0.00, and the maximum value is 999999999999.99.
From the left to the right, when the numbers are 4th digits in the integer, and the values of 8th and 12th are "0", they should not be converted to uppercase "0 ". The decimal place should not be zero.
Furthermore, if a digit is "0", if the former digit is "0", it should not be converted to "zero ", otherwise, the result is "zero.
However, this is an exception. For example, the third digit is "0" and the second digit is "0, it should be converted to "one hundred thousand yuan and one hundred yuan.
Therefore, we need to divide the 12-digit integer into three parts. If the number is less than 12 characters, add "0" to the front.
When a number in a certain part does not meet the condition that the previous digit is not "0", you must first check whether a number other than "0" exists in the current part, if yes, "zero" is not required ".
If the current part still does not meet the conditions, you need to continue to search for non-"0" numbers in the previous part. If it is found, add "zero ".
Note: When the integer is 0 and the first digit of the decimal part is "0", do not convert it to "zero ".
Using System; using System. collections. generic; using System. globalization; using System. text; using System. text. regularExpressions; using System. threading; namespace Demo {internal class Program {private static Dictionary <char, string> _ dic = new Dictionary <char, string> (); public static void Main (string [] args) {_ dic ['1'] = "one"; _ dic ['2'] = "two"; _ dic ['3'] = "three "; _ dic ['4'] = "Si"; _ dic ['5'] = "Wu"; _ di C ['6'] = "land"; _ dic ['7'] = "Beijing"; _ dic ['8'] = "Beijing "; _ dic ['9'] = "medium"; _ dic ['0'] = "zero"; while (true) {Test (); Thread. sleep (1000) ;}} private static void Test () {Random r = new Random (); int count = r. next (0, 13); int I = 0; StringBuilder sb = new StringBuilder (); while (I ++ <count) {sb. append (r. next (0, 10);} sb. append (". "); I = 0; while (I ++ <2) {sb. append (r. next (0, 10);} Console. writeLine (Co NverToRMB (Convert. toDecimal (sb. toString (), CultureInfo. invariantCulture);} private static string ConverToRMB (decimal num) {string data = Convert. toString (num, CultureInfo. invariantCulture); bool isNum = ValidateData (data); if (! IsNum) {return "Invalid Number";} string [] parts = data. split ('. '); string intPart = parts [0]; string decPart = parts [1]; string intResult = ParseIntPart (intPart); bool isZero = String. isNullOrEmpty (intResult); string decResult = ParseDecPart (decPart, isZero); string result = string. empty; if (String. isNullOrEmpty (intResult) {if (String. isNullOrEmpty (decResult) {result = "Zero RMB";} else {result = decrere Sult ;}} else {if (String. isNullOrEmpty (decResult) {result = intResult + "";} else {result = intResult + "" + decResult;} return result ;} private static bool ValidateData (string data) {Regex r = new Regex (@ "^ \ d {0, 12 }\. \ d {2} $ "); return r. isMatch (data);} private static string ParseIntPart (string intPart) {char [] arr = intPart. toCharArray (); char [] temp = {'0', '0', '0', '0', '0', '0' ', '0', '0', '0', '0', '0', '0'}; arr. copyTo (temp, 12-arr. length); StringBuilder sb = new StringBuilder (); for (int I = 0; I <3; I ++) {StringBuilder s = new StringBuilder (); bool flag = false; for (int j = 0; j <4; j ++) {char c = temp [j + I * 4]; if (c! = '0') {s. append (_ dic [c]); switch (j) {case 0: s. append ('authorization'); break; case 1: s. append ('authorization'); break; case 2: s. append ('pick'); break;} else {if (j! = 3) {if (j! = 0 & temp [j + I * 4-1]! = '0') {for (int k = j; k <4; k ++) {if (temp [k + I * 4]! = '0') {s. append ("zero"); break ;}} else {if (I> 0) {for (int k = j; k> = 0; k --) {if (temp [k + I * 4]! = '0') {flag = true; break ;}} if (! Flag) {bool flag2 = false; for (int k = I-1; k> = 0; k --) {if (! Flag2) {for (int m = 0; m <4; m ++) {if (temp [m + k * 4]! = '0') {s. append ("zero"); flag2 = true; flag = true; break ;}}}}}} if (s. length> 0) {switch (I) {case 0: s. append ('66'); break; case 1: s. append ('wan'); break;} sb. append (s) ;}} return sb. toString ();} private static string ParseDecPart (string decPart, bool isZero) {char [] arr = decPart. toCharArray (); char c1 = arr [0]; char c2 = arr [1]; if (c1 = '0' & c2 = '0 ') {return string. empt Y;} StringBuilder sb = new StringBuilder (); if (c1! = '0') {sb. Append (_ dic [c1]); sb. Append ('angular ');} else {if (! IsZero) {sb. Append ("zero") ;}} if (c2! = '0') {sb. Append (_ dic [c2]); sb. Append ('die');} return sb. ToString ();}}}
Published in my blog at the same time