This program is used to convert lowercase numbers to general Chinese uppercase or Chinese capitalization, providing the following functions: 1. Allow any integer or decimal, general Chinese capital conversion range between (-10,000,000,000,000 ~ 10,000,000,000,000 ,000), Chinese capital amount conversion range (0, nort,000,000) between (0, "), such as" 0 "," 0. "," .0 "," -.0 "," .0- "," 114 " , "20. ", etc .2. Can identify and process any error input. 3. Algorithm is stable, fast, Chinese capital is more in line with language habits
Using system; using system.text;
Namespace DigitTochntext {/// // This program is used to convert lowercase numbers to /// 1. General Chinese capital Digital /// 2. RMB uppercase digital /// algorithm design: Huang Jing /// Program production: Huang /// Time: August 12, 2004 /// Class DigitTochntext {Private Readonly Char [] chnGENTEXT; Private Readonly char [] chngenDigit;
Private readonly char [] chnrmbtext; private readonly char [] chnrmbdigit; private readonly char [] chnrmbunit;
// // Constructor // public DigitTochnText () {// General capital Chinese numeric group chnGENTEXT = New char [] {'zero', 'one', 'two', 'three', 'four', 'five' , 'Six', 'seven', 'eight', 'nine'}; chngenDigit = new char [] {'ten', 'hundred', '千', '10 million,' billion '};
// RMB special numeric group chnrmbtext = new char [] {'zero', '壹', '', '', '', 'Wu', 'Lu', 'Dye', '捌', ' '}; Chnrmbdigit = new char [] {' picking ',' ',' 仟 ',' 10,000 '}; chnrmbunit = new char [] {' angle ',' points'};}
// // Main Conversion Function // Parameter // String Strdigit - Stand String // Bool BTORMB - Whether to convert to RMB // Back // String - Convert Cyber String // Public String Convert (String strDigit , BOOL BTORMB) {// Check Input Digital Validity Checkdigit (Ref strdigit, BTORMB);
// Define the result string StringBuilder strresult = new stringbuilder ();
// Extract the symbol section Extractsign (Ref strRRRESULT, REF strdigit, btormb);
// Extract and convert integers and decimal parts ConvertNumber (REF STRESULT, REF strdigit, btormb); return strresult.toString ();
// // Convert Number // Protected Void ConvertNumber (Ref StringBuilder Strresult, Ref string strdigit, bool btormb) {int indexofpoint; if (-1 == (indexofpoint = strdigit.indexof ('.'))) // If not Extra number {Strresult.Append (ConvertIntegRal (strdigit, btormb));
If (btormb) // If converted to RMB {Strresult.Append ("round,");}} else // has a fractional portion {// first convert integer part IF (0 == indexofpoint) // If "" The first character {if (! Btormb) // If converted into a general Chinese capital {strresult.Append ('zero');}} else // If "." Is not the first character {Strresult.Append (Convertintegage . Substring (0, indexofpoint), btormb);
/ / Re-convert the fractional portion IF (strdigit.length - 1! = Indexofpoint) // If "is not the last character {if (btormb) // If converted into RMB {IF (0! = Indexofpoint) // If" "Not the first character {if (1 == Strresult.Length &&" zero "== Strresult.toString ()) // If the integer part is just '0' {strresult.remove (0, 1); // remove "Zero"} else {strresult.Append ('circle');}}}}}}}}}}}}}}
String stramp = converTfractional (Strdigit.Substring (INDEXOFPOINT 1), BTORMB);
If (0! = strmp.length) // Decimal part has a return value {if (btormb && // If converted to RMB 0 == Strresult.Length && // without integer part "zero" == startmp.substring (0 1)) // and the first character of the return string is "zero" {strresult.Append (stramp.substring (1));} else {strresult.Append (strtmp);}}
If (btormb) {if (0 == strresult.length) // If the result string does not have {Strresult.Append ("zero round whole");} // If the result string ends in "round" ELSE IF ("Round" == Strresult.toString (). Substring (Strresult.Length - 1, 1)) {strresult.append ('whole');}}} else if (btormb) // If "is the last one Character and convert to RMB {Strresult.Append ("round");}}} // // Check Input Digital Validity // Private Void CHECKDIGIT (REF STRING STRDIGIT, BOOL BTORMB) {DECIMAL DEC; TRY {DEC = DECIMAL.PARSE (STRDIGIT);} catch ("Formatexception) {throw new exception (" Enter the format of the number is incorrect. ");} catch (exception e) {throw E;} if (btormb) // If converted into RMB { IF (DEC> = 10000000000000000M) {throw new exception ("Enter the number is too large, beyond the range.");} else if (DEC <0) {throw new exception ("Do not allow RMB is negative.");}} ELSE // If converted into Chinese capital {IF (DEC <= -10000000000000000000000M) {throw new exception ("Input Number is too large or too small, beyond the range.");}}}}
// // Extract the symbol of the input string // protected void extractsign (Ref strodbuilder strresult, ref string strdigit, bool btormb) {// ' ' at the first IF (" " == strdigit.substring (0, 1) ) {Strdigit = strdigit.substring (1);} // '-' in the forefrive else if ("-" == strdigit.substring (0, 1)) {if (! Btormb) {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)) {if (! btormb) {strresult.Append ('negative " ); STRDIGIT = strdigit.substring (0, strdigit.length - 1);}} // // Conversion integer part // protected string controlintegral (String strintegral, bool btormb) {// remove all of the numbers All '0' / / And divide the numbers to Char [] Integral = (long.parse (Strintegral) .toChararray ();
// Define Results Strings StringBuilder Strint = New StringBuilder (); int Digit; Digit = Integral.Length - 1;
// Use the correct reference char [] chntext = btormb? Chnrmbtext: chnGEntext; char [] chngendiGit; / / turn into Chinese digital and add Chinese digits // to process all digital INTs for the highest bit to ten i; for (i = 0; I // Add digital IF (0 == Digit% 4) // '10 million or' billion '{if (4 == DIGIT || 12 == Digit) {strint.append (chndigit [3]); //' Wan '} else if (8 == Digit) {strint.append (chndigit [4]); //' billion '}} else //' 10 ',' 100 'or' 千 '{strint.append (chndigit [ Digit% 4 - 1]);} Digit ---; / / If the number is not '0' // or only one digit // adds the corresponding Chinese digital IF ('0'! = Integral [integral.length - 1] || 1 == integral.length) { Strint.Append (chntext [integral [i] - '0']); // Traverse the entire string i = 0; string straTemp; // temporarily store string int J; // Find "zero X" structure with bool bdosomething; // find "万" or "亿" is true While (i / / Find all connected "zero X" structure while (j J = 2; If (j! = i) // If you find a "zero" structure of "万" or "iodie" structure, 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;} // Pointer 2-bit i = 2 each time;} // encountered "100 million" to become "100 million" or "100 million" straTemP = chndigit [4] .tostring () chndigit [3] .tostring (); // Define string "100 million" or "billion" Wan "int index = strint.toString (). Indexof (strtemp); if (-1! = Index) {if (Strint.Length - 2! = Index && // If" hundreds of thousands "is not at the end (Index 2 < Strint.LENGTH && "zero"! = strint.toString (). Substring (index 2, 1)))) // and thereafter no "zero" {strint = strint.replace (strTemp, chndigit [4] .tostring ), index, 2); // variable "hundred million" "100 million" strint = strint.insert (index 1, "zero");} else // If "hundred million" is at the end or thereafter There is "zero" {strint = strint.replace (strTemp, chndigit [4] .tostring (), index, 2); // change "hundred million" is "100 million"} If (! btormb) // If converted to a general Chinese capital {// starting to "10" change to "ten" if (strint.length> 1 && "10" == strint.toString (). Substring (0 , 2)) {strint = strint.remove (0, 1);}} Return strint.toString (); // // Convert fractional part // protected string converTfractional (String strfractional, bool btormb) {char [] FRACTIONAL = strfractional.tochararray (); Stringbuilder strfrac = new stringbuilder (); / / Becomes Chinese digital INT i; if (btormb) // If converted to RMB {for (i = 0; i // If the beginning of "zero angle" == strfrac.tostring (). Substring (0, 2)) {// If only "zero angle" IF (2 == strfrac.length) {Strfrac.remove (0, 2);} else // If there is "x points", delete "angle" {strfrac.remove (1, 1);}}} else // If converted to a general Chinese capital { For (i = 0; i Return strfrac.tostring (); // // Application main function entry point /// [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 ("Chinese Upperword Number: {0}", Obj.convert (Str, False));} Catch (Exception E) {Console.Writeline ("Converters When you write, error: {0}", E.MESSAGE } Try {console.writeline ("RMB uppercase is: {0} / n", obj.convert (str, true));} catch (exception e) {console.writeline ("Error when converting RMB: {0}", E.MESSAGE); Console.writeLine (); }}}}