Use C # To convert Arabic numerals to uppercase Chinese

Source: Internet
Author: User

Using System;

Namespace ConsoleApp
{
/// <Summary>
/// This class converts Arabic numerals to uppercase Chinese
/// This class does not identify illegal numbers
/// Call the NumToChn Method
/// </Summary>
Public class NumFormat
{
Public NumFormat ()
{
//
// TODO: add the constructor logic here
//
}

// Convert numbers
Private char ToNum (char x)
{
String strChnNames = "0 August 5, 1234 ";
String strNumNames = "0123456789 ";
Return strChnNames [strNumNames. IndexOf (x)];
}

// Convert an integer smaller
Private string ChangeInt (string x)
{
String [] strArrayLevelNames = new string [4] {"", "Ten", "Hundred", "Thousand "};
String ret = "";
Int I;
For (I = x. Length-1; I> = 0; I --)
If (x [I] = '0 ')
Ret = ToNum (x [I]) + ret;
Else
Ret = ToNum (x [I]) + strArrayLevelNames [x. Length-1-i] + ret;
While (I = ret. IndexOf ("0 "))! =-1)
Ret = ret. Remove (I, 1 );
If (ret [ret. Length-1] = '0' & ret. Length> 1)
Ret = ret. Remove (ret. Length-1, 1 );
If (ret. Length> = 2 & ret. Substring () = "10 ")
Ret = ret. Remove (0, 1 );
Return ret;
}

// Convert the integer
Private string ToInt (string x)
{
Int len = x. Length;
String ret, temp;
If (len <= 4)
Ret = ChangeInt (x );
Else if (len <= 8)
{
Ret = ChangeInt (x. Substring (0, len-4) + ";
Temp = ChangeInt (x. Substring (len-4, 4 ));
If (temp. IndexOf ("thousands") =-1 & temp! = "")
Ret + = "zero" + temp;
Else
Ret + = temp;
}
Else
{
Ret = ChangeInt (x. Substring (0, len-8) + ";
Temp = ChangeInt (x. Substring (len-8, 4 ));
If (temp. IndexOf ("thousands") =-1 & temp! = "")
Ret + = "zero" + temp;
Else
Ret + = temp;
Ret + = "Ten Thousand ";
Temp = ChangeInt (x. Substring (len-4, 4 ));
If (temp. IndexOf ("thousands") =-1 & temp! = "")
Ret + = "zero" + temp;
Else
Ret + = temp;
}
Int I;
If (I = ret. IndexOf ("0 thousand "))! =-1)
Ret = ret. Remove (I + 1, 1 );
While (I = ret. IndexOf ("0 "))! =-1)
Ret = ret. Remove (I, 1 );
If (ret [ret. Length-1] = '0' & ret. Length> 1)
Ret = ret. Remove (ret. Length-1, 1 );
Return ret;
}

Private string ToDecimal (string x)
{
String ret = "";
For (int I = 0; I <x. Length; I ++)
Ret + = ToNum (x [I]);
Return ret;
}

Public string NumToChn (string x)
{
If (x. Length = 0)
Return "";
String ret = "";
If (x [0] = '-')
{
Ret = "negative ";
X = x. Remove (0, 1 );
}
If (x [0]. ToString () = ".")
X = "0" + x;
If (x [x. Length-1]. ToString () = ".")
X = x. Remove (x. Length-1, 1 );
If (x. IndexOf (".")>-1)
Ret + = ToInt (x. substring (0, x. indexOf (". ") +" point "+ ToDecimal (x. substring (x. indexOf (". ") + 1 ));
Else
Ret + = ToInt (x );
Return ret;
}
}
}

Test Project

Using System;

Namespace ConsoleApp
{
Class MainClass
{
Static void Main (string [] args)
{
/*
System. Console. WriteLine ("Hello, The World! ");

System. Console. WriteLine ("My Love! ");

ClassTest ct = new ClassTest ();
System. Console. WriteLine (ct. Get_Str ());
*/

/*
// Overload Operator
MyVector v1 = new MyVector (5, 12 );
MyVector v2 = new MyVector (4, 3 );
MyVector v3 = new MyVector ();
V3 = v1 + v2;
System. Console. WriteLine ("{0} test", v3.Length );
*/

// Convert to a large number
NumFormat nf = new NumFormat ();
String x;
While (true)
{
Console. Write ("X = ");
X = Console. ReadLine ();
If (x = "") break;
Console. WriteLine ("{0 }={ 1}", x, nf. NumToChn (x ));
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.