Arabic numerals in accounting change to traditional Chinese capital and Arabic capital
// Note
// Conversion Tool class
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace IntegrationSystemChemicalIndustry. Product
{
/// <Summary>
/// Arabic numerals are converted to Chinese numerals, and Chinese numerals are converted to Arabic numerals.
/// By hcling97.Net 2007.03
/// </Summary>
Public class NumberConventer
{
Private string [] ArabinNum = {"0", "1", "2", "3", "4", "5", "6", "7 ", "8", "9 "};
Private string [] ChnNum = {"zero", "one", "two", "three", "Si", "Wu", "Lu", "Lu ", "weight "};
Private string [] Union = {"", "Pick", "random", "random "};
Public NumberConventer ()
{
//
// TODO: add the constructor logic here
//
}
Public string ArabToChn (Decimal ArabNum, out string msg)
{
String neg = string. Empty;
Neg = (ArabNum <0 )? "Negative ":"";
String result = string. Empty;
String [] part =
(ArabNum. ToString (). Replace ("-", string. Empty). Split ('.');
Int64 temp = Convert. ToInt64 (part [0]);
Int64 epart = temp;
String dotpart =
(Part. Length> 1 )? Part [1]: string. Empty;
If (part. Length> 1)
{
Dotpart = this. GetDotPart (dotpart );
}
String tmp = string. Empty;
String lasttemp = string. Empty;
For (int I = 0; I <= (epart. ToString (). Length-1)/4); I ++)
{
Int thousand = 0;
Thousand = Convert. ToInt32 (temp %
10000 );
Temp = temp/10000;
Lasttemp = tmp;
Tmp = this. GetThousandPart (thousand );
If (I = 0)
{
Result = tmp;
Lasttemp = tmp;
}
If (I = 1) // The returned value is.
{
If (result = "0 ")
{
Result = string. Empty;
}
Result = tmp + "Ten Thousand" +
(Lasttemp. IndexOf ("thousands") =-1 & lasttemp! = "Zero ")? "Zero": "") + result;
}
If (I = 2) // million
{
If (result. IndexOf ("0 thousand ")! =-1)
{
Result = result. Replace ("0 thousand", string
. Empty );
}
Result = tmp + "" + (lasttemp. IndexOf ("thousands") =-1 & lasttemp! = "Zero ")? "Zero": "") + result;
}
If (I = 3) // trillion
{
If (result. IndexOf ("0 billion ")! =-1)
{
Result = result. Replace ("0 billion ");
}
Result = tmp + "Ten Thousand" + (lasttemp. IndexOf ("thousands") =-1 & lasttemp! = "Zero ")? "Zero": "") + result;
}
If (I = 4) // hundreds of millions
{
If (result. IndexOf ("0 thousand ")! =-1)
{
Result = result. Replace ("0 thousand", string
. Empty );
}
Result = tmp + "" + (lasttemp. IndexOf ("thousands") =-1 & lasttemp! = "Zero ")? "Zero": "") + result;
}
}
Result = neg + result + dotpart;
Msg = "converted! ";
Return result;
}
/// <Summary>
/// Process the fractional part
/// </Summary>
/// <Param name = "dotPart"> </param>
/// <Returns> </returns>
Private string GetDotPart (string dotPart)
{
String result = "point ";
For (int I = 0; I <dotPart. Length; I ++)
{
Result + = ChnNum [Convert. ToInt32 (dotPart [I]. ToString ()];
}
For (int j = 0; j <result. Length; j ++)
// Remove invalid zero or point
{
If (result [result. Length-j-1]. ToString ()! = "Point" & result [result. Length-j-1]. ToString ()! = "0 ")
{
Break;
}
Else
{
Result = result. Substring (0, (result. Length-j-1 ));
}
}
Return result;
}
/// <Summary>
/// Analysis of less than characters
/// </Summary>
/// <Returns> </returns>
Private string GetThousandPart (int number)
{
If (number = 0)
{
Return "zero ";
}
String result = string. Empty;
Bool lowZero = false;
// Whether the non-zero value is found in the low position of the record and true is not found
Bool befZero = false;
// Indicates whether the first digit is a non-zero value. If the value is 0, the value is set to true.
Int temp = number;
Int index = number. ToString (). Length;
For (int I = 0; I <index; I ++)
{
Int n = temp % 10;
Temp = temp/10;
If (I = 0) // start position
{
If (n = 0)
{
LowZero = true; // 0 for Low Position
BefZero = true; // The frontend is 0.
}
Else
{
Result = ChnNum [n];
}
}
Else
{
If (n! = 0)
{
Result = ChnNum [n] + Union [I] + result;
LowZero = false;
BefZero = false;
}
Else
{
If (! LowZero)
{
If (! BefZero)
// There are several low positions, and the front position is not 0, and the base value is 0.
// Eg.5906
{
Result = ChnNum [n] + result;
BefZero = true;
}
Else
// There are several low positions, and the front position is 0, which is omitted from the zero eg. 5008
{
}
}
Else // the low position is 0.
{
If (! BefZero) // theoretically, eg 5080 does not exist
{
Result = ChnNum [n] + result;
BefZero = true;
}
Else // eg. 5000
{
}
}
}
}
}
Return result;
}
Public Decimal ChnToArab (string ChnNum)
{
Decimal result = 0;
String temp = ChnNum;
Bool neg = false;
If (ChnNum. IndexOf ("negative ")! =-1)
{
Neg = true;
Temp = temp. Replace ("negative", string. Empty );
}
String pre = string. Empty;
String abo = string. Empty;
Temp = temp. Replace ("point ",".");
String [] part = temp. Split ('.');
Pre = part [0];
Decimal dotPart = 0;
If (part. Length> 1)
{
Abo = part [1];
DotPart = this. GetArabDotPart (abo );
}
Int yCount = 0;
// Hundreds of millions.
// Int yPos = 0;
Int index = 0;
While (index <pre. Length)
{
If (pre. IndexOf ("", index )! =-1)
{
YCount ++;
// YPos = pre. IndexOf ("", index );
Index = pre. IndexOf ("", index) + 1;
}
Else
{
Break;
}
}
If (yCount = 2) // hundreds of millions
{
Pre = pre. Replace (" ",",");
String [] sp = pre. Split (',');
Result =
(Neg? -1: 1) * (this. handlePart (sp [0]) * 10000000000000000) + (this. handlePart (sp [1]) * 100000000) + this. handlePart (sp [2]) + dotPart;
}
Else
{
If (yCount = 1)
{
Pre = pre. Replace (" ",",");
String [] sp = pre. Split (',');
Result = (neg? -1: 1) * (this. HandlePart (sp [0]) * 100000000) + this. HandlePart (sp [1]) + dotPart;
}
Else
{
If (yCount = 0)
{
Result = (neg? -1: 1) * this. HandlePart (pre) + dotPart;
}
}
}
Return result;
}
/// <Summary>
/// Process less than million content.
/// </Summary>
/// <Param name = "num"> </param>
/// <Returns> </returns>
Private Decimal HandlePart (string num)
{
Decimal result = 0;
String temp = num;
Temp = temp. Replace ("Ten Thousand ",",");
String [] part = temp. Split (',');
For (int I = 0; I <part. Length; I ++)
{
Result + = Convert. toDecimal (this. getArabThousandPart (part [part. length-I-1]) * Convert. toDecimal (System. math. pow (10000, Convert. toDouble (I ))));
}
Return result;
}
/// <Summary>
/// Obtain the Arabic decimal part.
/// </Summary>
/// <Returns> </returns>
Private Decimal GetArabDotPart (string dotpart)
{
Decimal result = 0.00 M;
String spe = "0 .";
For (int I = 0; I <dotpart. Length; I ++)
{
Spe + = this. switchNum (dotpart [I]. ToString (). ToString ();
}
Result = Convert. ToDecimal (spe );
Return result;
}
Public int GetArabThousandPart (string number)
{
String ChnNumString = number;
If (ChnNumString = "0 ")
{
Return 0;
}
If (ChnNumString! = String. Empty)
{
If (ChnNumString [0]. ToString () = "10 ")
{
ChnNumString = "1" + ChnNumString;
}
}
ChnNumString = ChnNumString. Replace ("zero", string. Empty );
// Remove all zeros
Int result = 0;
Int index = ChnNumString. IndexOf ("thousands ");
If (index! =-1)
{
Result + = this. switchNum (ChnNumString. Substring (0, index) * 1000;
ChnNumString = ChnNumString. Remove (0, index + 1 );
}
Index = ChnNumString. IndexOf ("Hundred ");
If (index! =-1)
{
Result + = this. switchNum (ChnNumString. Substring (0, index) * 100;
ChnNumString = ChnNumString. Remove (0, index + 1 );
}
Index = ChnNumString. IndexOf ("10 ");
If (index! =-1)
{
Result + = this. switchNum (ChnNumString. Substring (0, index) * 10;
ChnNumString = ChnNumString. Remove (0, index + 1 );
}
If (ChnNumString! = String. Empty)
{
Result + = this. switchNum (ChnNumString );
}
Return result;
}
/// <Summary>
/// Obtain the Arabic numerals corresponding to the Chinese characters
/// </Summary>
/// <Param name = "n"> </param>
/// <Returns> </returns>
Private int switchNum (string n)
{
Switch (n)
{
Case "zero ":
{
Return 0;
}
Case "1 ":
{
Return 1;
}
Case "2 ":
{
Return 2;
}
Case "3 ":
{
Return 3;
}
Case "4 ":
{
Return 4;
}
Case "5 ":
{
Return 5;
}
Case "6 ":
{
Return 6;
}
Case "7 ":
{
Return 7;
}
Case "8 ":
{
Return 8;
}
Case "9 ":
{
Return 9;
}
Default:
{
Return-1;
}
}
}
}
}
/// Page call Method
NumberConventer NC = new NumberConventer (); // create a work Class Object
String TP = string. Empty; // TP Conversion Result
This.txt TotalPriceUP. Text = NC. ArabToChn (Convert. ToDecimal (_ TotalPrice), out TP); // The result returned by this method. It is of the string type and can process hundreds of millions of digits.