This program is used to convert lowercase digital to Chinese capital, which provides the following functions: 1. Allow any integer or decimal, range between (-10,000,000,000 to 10,000,000,000,000). Include "0.", ".0", "-.0 "," .0- "," 114 "," 20. ", etc ..2. Can identify and process any error input 3. Algorithm is stable, fast, Chinese capital is more qualitative language habits
Using system; using system.text;
Namespace DigitTochnText {///
/// This program is used to turn lowercase numbers into uppercase Chinese digital /// algorithm design: Huangjing /// Program production: Huang Jing /// Time: August 12, 2004 /////
Class DigitTochntext {Private Readonly Char [] Chntext; Private Readonly Char [] chndigit;
Public DigitTochnText () {chntext = new char [] {'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine '}; Chndigit = new char [] {' 10 ',' 100 ',' Qian ',' Wan ',' billion '};}
Public String Convert (String strdigit) {// Check Input Digital Decimal Dec; try {dec = decimal.Parse (strdigit);} catch (formatexception) {throw new exception ("Enter the format is incorrect.");} catch (Exception e) {throw e;}
IF (DEC <= -10000000000000000M || DEC> = 10000000000000000m) {throw new exception ("Input Number is too large or too small, beyond the range.");}
Stringbuilder strresult = new stringbuilder ();
// Extract symbol section // ' ' at the forefront IF (" " == strdigit.substring (0, 1)) {strdigit = strdigit.substring (1);} // '-' in the forefrone else if (" - "== strdigit.substring (0, 1)) {strresult.Append ('negative'); strdigit = strdigit.substring (1);} // ' ' at the last ELSE IF (" "== Strdigit. Substring (strdigit.length - 1, 1)) {strdigit = strdigit.substring (0, strdigit.length - 1);} // '-' in the last ELSE IF ("-" == strdigit.substring (strdigit.length - 1, 1)) {Strresult.Append ('negative'); strdigit = strdigit.substring (0, strdigit.length - 1);} // Extract integer and decimal part int indexofpoint; if (-1 == (IndexoftPoint = strdigit.indexof ('.')))) // If there is no fractional portion {StrResult.Append (Convertintegral (strdigit));} else // has a fractional portion {// first convert integer part IF (0 == IndexOfPoint) / / If "." Is the first character {strresult.Append ('zero');} else {strresult.Append (strdigit.substring (0, indexofpoint));}
/ / Re-convert the fractional portion IF (strdigit.length - 1! = Indexofpoint) // If "." Is not the last character {strresult.Append ('point'); strresult.Append (strdigit.substring (Indexoft 1 ))));}}
Return strresult.toString ();
// Convert Integer Part Protected String ConvertIntegral (String Strintegral) {// Removes all of the numbers in front of all '0' // and divide the number to Char [] integral = (long.parse (long.parstegral) .tostring ))) .Tochararray ();
/ / Turn into Chinese Digital and add Chinese digital StringBuilder strint = new stringbuilder ();
INT I; int Digit; Digit = Integral.Length - 1;
// Test all the digital for (i = 0; i Digit--; / / If the number is not '0' // or a bit number of '0' but only one digit // adds corresponding Chinese digital IF ('0'! = Integral [integral.length - 1] || 1 == Integral.Length) {strint.append (chntext [integral [i] - '0']);} // Traverse the entire string i = 0; While (i // Find all connected "zero X" structure While (j J = 2; IF (j! = i) // If the "zero X" structure is found, all delete {strint = strint.remove (i, j - i); In addition to the end of the most, or after "万" or "零" is not "zero", it is added to the "zero" IF (i <= strint.length - 1 &&! Bdosomething) { Strint = Strint.insert (i, 'zero'); i ;}} If (bdosomething) // If you find "万" or "亿" structure {strint = strint.remove (i, 1); // remove 'Zero' i ; Continue;} / / The pointer can be moved 2-bit i = 2;} // encountered "100 million" to become "100 million" or "billion" int index = strint.toString (). Indexof ("100 million"); IF (-1! = index) {IF (Strint.Length - 2! = index && // If "hundreds of thousands" is not in the end (Index 2 / / The beginning is "10" to "Ten" IF (strint.length> 1 && "10" == strint.toString (). Substring (0, 2)) {strint = strint.remove (0, 1 } Return strint.toString (); // Convert the fractional part protected string converTfractional (String strfractional) {char [] FRACTIONAL = strfractional.tochararray (); StringBuilder strfrac = new stringbuilder (); / / Becomes Chinese digital INT i; for (i = 0; i Return strfrac.tostring (); /// /// The main entry point for the application. /// [Stathread] static void main (string [] args) {DigitTochnText obj = new digittochntext (); String str = ""; while (""! = Str) {Console.write ("Please enter lowercase numbers:"); if ("== (str = console.readline ())) Break; try {console. WriteLine ("Upperword Chinese Number: {0} / N", Obj.convert (Str));} Catch (Exception E) {Console.WriteLine ("Error: {0} / n", E.MESSAGE); Continue }}}}}}