C # Implementing numeric amounts converting uppercase amounts

Source: Internet
Author: User
Tags numeric split strlen

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");

its code as follows: ///converts the numeric amount main function (including decimals)


     ///


///Numeric String


///converts to a string that is capitalized in Chinese 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


string[] Splitstr=null; Defines an array of strings separated by a decimal point


Splitstr=str. Split (Ch[0]);//split string by decimal


if (splitstr. length==1)//Only integer part


return Convertdata (str) + "round whole";


else//has a small number of parts


        {


string rstr;


Rstr=convertdata (splitstr[0]) + "round";//Convert integer part


Rstr+=convertxiaoshu (splitstr[1]);/Convert decimal parts


return RSTR;


        }


     }





      ///


///to determine whether a positive numeric string


     ///


///judgment String


///Returns True if it is a number, otherwise 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)


///www.bianceng.cn


///Integer numeric string that needs to be converted


///converted to a string in Chinese capital


public string Convertdata (String str)


     {


string Tmpstr= "";


string Rstr= "";


int Strlen=str. Length;


if (strlen<=4)//digital 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


//The number of two conversions plus the


rstr= String.Concat (ConvertDigit (TMPSTR) + "Million", RSTR);


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), and then intercept four digits


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;


     }





      ///


///Conversion Number (decimal part)


     ///


///the number of decimal parts that need to be converted


///converted to a string in Chinese capital


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 Digital


     ///


///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 digital


     ///


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;


     }

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.