熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#中數字金額轉換大寫金額方法

2022-06-13   來源: .NET編程 
     代碼的主調函數為ConvertSum它的參數是string類型的數字字符串在ASPNET中調用方式如下:

  LabelText=ConvertSum(數字字符串);

  其代碼如下            /// 轉換數字金額主函數(包括小數)
          ///
          /// 數字字符串
          /// 轉換成中文大寫後的字符串或者出錯信息提示字符串
          public string ConvertSum(string str)
          {
               if(!IsPositveDecimal(str))
               return 輸入的不是正數字!;
               if(DoubleParse(str)>)
               return 數字太大無法換算請輸入一萬億元以下的金額;
               char[] ch=new char[];
               ch[]=; //小數點
               string[] splitstr=null; //定義按小數點分割後的字符串數組
               splitstr=strSplit(ch[]);//按小數點分割字符串
               if(splitstrLength==) //只有整數部分
               return ConvertData(str)+圓整;
               else //有小數部分
               {
                    string rstr;
                    rstr=ConvertData(splitstr[])+;//轉換整數部分
                    rstr+=ConvertXiaoShu(splitstr[]);//轉換小數部分
                    return rstr;
               }
          }

              ///
          /// 判斷是否是正數字字符串
          ///
          /// 判斷字符串
          /// 如果是數字返回true否則返回false
          public bool IsPositveDecimal(string str)
          {
               Decimal d;
               try
               {
                    d=DecimalParse(str);
               }
               catch(Exception)
               {
                   return false;
               }
               if(d>)
               return true;
               else
               return false;
          }

  

  ///
          /// 轉換數字(整數)
          ///
          /// 需要轉換的整數數字字符串
          /// 轉換成中文大寫後的字符串
          public string ConvertData(string str)
          {
               string tmpstr=;
               string rstr=;
               int strlen=strLength;
               if (strlen<=)//數字長度小於四位
               {
                    rstr= ConvertDigit(str);

  }
               else
               {
                    if (strlen<=)//數字長度大於四位小於八位
                    {
                         tmpstr=strSubstring(strlen);//先截取最後四位數字
                         rstr=ConvertDigit(tmpstr);//轉換最後四位數字
                         tmpstr=strSubstring(strlen);//截取其余數字
                         //將兩次轉換的數字加上萬後相連接
                         rstr= StringConcat(ConvertDigit(tmpstr)+rstr);
                         rstr=rstrReplace(零零);
                    }
                else
                if(strlen<=)//數字長度大於八位小於十二位
                {
                     tmpstr=strSubstring(strlen);//先截取最後四位數字
                     rstr=ConvertDigit(tmpstr);//轉換最後四位數字
                     tmpstr=strSubstring(strlen);//再截取四位數字
                     rstr= StringConcat(ConvertDigit(tmpstr)+rstr);
                     tmpstr=strSubstring(strlen);
                     rstr= StringConcat(ConvertDigit(tmpstr)+rstr);
                     rstr=rstrReplace(零億);
                     rstr=rstrReplace(零萬);
                     rstr=rstrReplace(零零);
                     rstr=rstrReplace(零零);
                }
               }
               strlen=rstrLength;
               if (strlen>=)
               {
                    switch(rstrSubstring(strlen))
                    {
                         case 佰零:rstr=rstrSubstring(strlen)+; break;
                         case 仟零:rstr=rstrSubstring(strlen)+; break;
                         case 萬零:rstr=rstrSubstring(strlen)+;break;
                         case 億零:rstr=rstrSubstring(strlen)+;break;
                    }
               }
               return rstr;
          }

              ///
          /// 轉換數字(小數部分)
          ///
          /// 需要轉換的小數部分數字字符串
          /// 轉換成中文大寫後的字符串
          public string ConvertXiaoShu(string str)
          {
               int strlen=strLength ;
               string rstr;
               if(strlen==)
               {
                    rstr=ConvertChinese(str)+;
                    return  rstr;
               }
               else
               {
                    string tmpstr=strSubstring();
                    rstr=ConvertChinese(tmpstr)+;
                    tmpstr=strSubstring();
                    rstr+=ConvertChinese(tmpstr)+;
                    rstr=rstrReplace(零分);
                    rstr=rstrReplace(零角);
                    return rstr;
               }
          }

              ///
          /// 轉換數字
          ///
          /// 轉換的字符串(四位以內)
          ///
          public string ConvertDigit(string str)
          {
               int strlen=strLength;
               string rstr=;
               switch (strlen)
               {
                    case : rstr=ConvertChinese(str);break;
                    case : rstr=ConvertDigit(str);break;
                    case : rstr=ConvertDigit(str);break;
                    case : rstr=ConvertDigit(str);break;
               }
               rstr=rstrReplace(拾零);
               strlen=rstrLength;
               return rstr;
          }

              ///
          /// 轉換四位數字
          ///
          public string ConvertDigit(string str)
          {
               string str=strSubstring();
               string str=strSubstring();
               string str=strSubstring();
               string str=strSubstring();
               string rstring=;
               rstring+=ConvertChinese(str)+;
               rstring+=ConvertChinese(str)+;
               rstring+=ConvertChinese(str)+;
               rstring+=ConvertChinese(str);
               rstring=rstringReplace(零仟);
               rstring=rstringReplace(零佰);
               rstring=rstringReplace(零拾);
               rstring=rstringReplace(零零);
               rstring=rstringReplace(零零);
               rstring=rstringReplace(零零);
               return rstring;
          }

              ///
          /// 轉換三位數字
          ///
          public string ConvertDigit(string str)
          {
               string str=strSubstring();
               string str=strSubstring();
               string str=strSubstring();
               string rstring=;
               rstring+=ConvertChinese(str)+;
               rstring+=ConvertChinese(str)+;
               rstring+=ConvertChinese(str);
               rstring=rstringReplace(零佰);
               rstring=rstringReplace(零拾);
               rstring=rstringReplace(零零);
               rstring=rstringReplace(零零);
               return rstring;
          }

              ///
          /// 轉換二位數字
          ///
          public string ConvertDigit(string str)
          {
               string str=strSubstring();
               string str=strSubstring();
               string rstring=;
               rstring+=ConvertChinese(str)+;
               rstring+=ConvertChinese(str);
               rstring=rstringReplace(零拾);
               rstring=rstringReplace(零零);
               return rstring;
          }

  

  ///
          /// 將一位數字轉換成中文大寫數字
          ///
          public string ConvertChinese(string str)
          {
               //零壹貳三肆伍陸柒捌玖拾佰仟萬億圓整角分
               string cstr=;
               switch(str)
               {
                    case : cstr=;break;
                    case : cstr=;break;
                    case : cstr=;break;
                    case : cstr=;break;
                    case : cstr=;break;
                    case : cstr=;break;
                    case : cstr=;break;
                    case : cstr=;break;
                    case : cstr=;break;
                    case : cstr=;break;
               }
               return(cstr);
          }


From:http://tw.wingwit.com/Article/program/net/201311/13431.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.