C # Implementation of the "Arabic numerals converted into Chinese capital amount" code

Source: Internet
Author: User
Tags integer numeric split strlen visual studio
Capital | amount | Convert the following C # code to achieve the ability to convert Arabic numerals into Chinese capital amounts, and Visual Studio.NET 2003 debugging Basic Pass (without rigorous testing, I hope you find errors after contact with the author fanz2000@sohu.com, or directly on the CSDN last post to inform).

The code's keynote function is convertsum, and its argument is a string of numeric strings of string type. The method is invoked in ASP.net as follows:
Label1.text=convertsum ("Numeric string");

The code is as follows:



Author: fanz2000
Email:fanz2000@sohu.com
///
Convert numeric amount main function (including decimal)
///
Numeric string
A string that is converted to a Chinese capital letter or an error message prompt string
public string Convertsum (String str)
{
if (! Ispositvedecimal (str))
Return "input is not a positive number! ";
if (Double.Parse (str) >999999999999.99)
Return "The number is too large to convert, please enter the amount of 1 trillion yuan below";
Char[] Ch=new char[1];
Ch[0]= '. '; Decimal point
String[] Splitstr=null; Defines an array of strings separated by a decimal point
Splitstr=str. Split (Ch[0]);//split string by decimal point
if (splitstr. length==1)///Only whole number of parts
return Convertdata (str) + "round whole";
else//has a small number of parts
{
String Rstr;
Rstr=convertdata (splitstr[0]) + "round";//Convert integer parts
Rstr+=convertxiaoshu (splitstr[1]);//Convert decimal parts
return RSTR;
}

}
///
Determines whether a positive numeric string
///
Judgment string
Returns true if it is a number, otherwise returns false
public bool Ispositvedecimal (string str)
{
Decimal D;
Try
{
D=decimal.parse (str);

}
catch (Exception)
{
return false;
}
if (d>0)
return true;
Else
return false;
}
///
Convert Number (integer)
///
Integer numeric string that needs to be converted
A string converted to uppercase in Chinese
public string Convertdata (String str)
{
String Tmpstr= "";
String Rstr= "";
int STRLEN=STR. Length;
if (strlen<=4)//number length is less than four bits
{
rstr= convertdigit (str);

}
Else
{

if (strlen<=8)//number length is greater than four bits, less than eight bits
{
Tmpstr=str. Substring (strlen-4,4)/First intercept the last four digits
Rstr=convertdigit (TMPSTR)//convert last four digits
Tmpstr=str. Substring (0,strlen-4)//intercept the remaining digits
Add the number of two conversions to the back of the link
Rstr= String.Concat (ConvertDigit (TMPSTR) + "Million", RSTR);
Rstr=rstr. Replace ("0 million", "million");
Rstr=rstr. Replace ("00", "0");

}
Else
if (strlen<=12)//number length is greater than eight bits, less than 12 bits
{
Tmpstr=str. Substring (strlen-4,4)/First intercept the last four digits
Rstr=convertdigit (TMPSTR)//convert last four digits
Tmpstr=str. Substring (strlen-8,4); four digits to intercept
Rstr= String.Concat (ConvertDigit (TMPSTR) + "Million", RSTR);
Tmpstr=str. Substring (0,strlen-8);
Rstr= String.Concat (ConvertDigit (TMPSTR) + "billion", Rstr);
Rstr=rstr. Replace ("0 billion", "billion");
Rstr=rstr. Replace ("0 million", "0");
Rstr=rstr. Replace ("00", "0");
Rstr=rstr. Replace ("00", "0");
}
}
Strlen=rstr. Length;
if (strlen>=2)
{
Switch (RSTR. Substring (strlen-2,2))
{
Case "Bai 0": rstr=rstr. Substring (0,strlen-2) + "Bai"; Break
Case "Thousand 0": Rstr=rstr. Substring (0,strlen-2) + "thousand"; Break
Case "Zero": Rstr=rstr. Substring (0,strlen-2) + "million";
Case "billion zero": Rstr=rstr. Substring (0,strlen-2) + "billion";

}
}

return RSTR;
}
///
Convert number (decimal part)
///
Decimal Part number string that needs to be converted
A string converted to uppercase in Chinese
public string Convertxiaoshu (String str)
{
int STRLEN=STR. Length;
String Rstr;
if (strlen==1)
{
Rstr=convertchinese (str) + "angle";
return RSTR;
}
Else
{
String Tmpstr=str. Substring (0,1);
Rstr=convertchinese (TMPSTR) + "angle";
Tmpstr=str. Substring (1,1);
Rstr+=convertchinese (TMPSTR) + "min";
Rstr=rstr. Replace ("0 points", "");
Rstr=rstr. Replace ("0 Horns", "");
return RSTR;
}


}

///
Convert numbers
///
Converted string (within four digits)
///
public string ConvertDigit (String str)
{
int STRLEN=STR. Length;
String Rstr= "";
Switch (strlen)
{
Case 1:rstr=convertchinese (str);
Case 2:rstr=convert2digit (str);
Case 3:rstr=convert3digit (str);
Case 4:rstr=convert4digit (str);
}
Rstr=rstr. Replace ("titbits", "Pick up");
Strlen=rstr. Length;

return RSTR;
}


///
Convert four-bit numbers
///
public string Convert4digit (String str)
{
String Str1=str. Substring (0,1);
String Str2=str. Substring (1,1);
String Str3=str. Substring (2,1);
String Str4=str. Substring (3,1);
String Rstring= "";
Rstring+=convertchinese (STR1) + "thousand";
Rstring+=convertchinese (str2) + "Bai";
Rstring+=convertchinese (STR3) + "Pick up";
Rstring+=convertchinese (STR4);
Rstring=rstring. Replace ("0 thousand", "0");
Rstring=rstring. Replace ("0 bai", "0");
Rstring=rstring. Replace ("0 pick Up", "0");
Rstring=rstring. Replace ("00", "0");
Rstring=rstring. Replace ("00", "0");
Rstring=rstring. Replace ("00", "0");
return rstring;
}
///
Convert three-bit numbers
///
public string Convert3digit (String str)
{
String Str1=str. Substring (0,1);
String Str2=str. Substring (1,1);
String Str3=str. Substring (2,1);
String Rstring= "";
Rstring+=convertchinese (str1) + "Bai";
Rstring+=convertchinese (STR2) + "Pick up";
Rstring+=convertchinese (STR3);
Rstring=rstring. Replace ("0 bai", "0");
Rstring=rstring. Replace ("0 pick Up", "0");
Rstring=rstring. Replace ("00", "0");
Rstring=rstring. Replace ("00", "0");
return rstring;
}
///
Convert two-bit numbers
///
public string Convert2digit (String str)
{
String Str1=str. Substring (0,1);
String Str2=str. Substring (1,1);
String Rstring= "";
Rstring+=convertchinese (STR1) + "Pick up";
Rstring+=convertchinese (STR2);
Rstring=rstring. Replace ("0 pick Up", "0");
Rstring=rstring. Replace ("00", "0");
return rstring;
}
///
Converts a digit to a Chinese capital number
///
public string Convertchinese (String str)
{
"0 One and three Woolu Qi Ba nine thousand million round the whole point"
String Cstr= "";
Switch (STR)
{
Case "0": cstr= "0";
Case "1": cstr= "one";
Case "2": cstr= "II";
Case "3": cstr= "three";
Case "4": cstr= "Restaurant";
Case "5": Cstr= "WU";
Case "6": Cstr= "Lu";
Case "7": cstr= "Qi";
Case "8": cstr= "BA";
Case "9": Cstr= "Nine";
}
return (CSTR);
}





Related Article

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.