博客园  :: 首页  :: 新随笔  :: 联系 :: 管理

数字转换人民币的类

Posted on 2008-11-22 01:05  codingsilence  阅读(191)  评论(0编辑  收藏  举报

 

 

 

首先是最NB的:

        public string GetMoney(double dd)
        {
           
string s = dd.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
           
string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L/.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[/.]|$))))", "${b}${z}");
           
string value = Regex.Replace(d, ".", delegate(Match m)
              {
return "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟萬億兆京垓秭穰"[m.Value[0] - '-'].ToString(); });
           
return value;
        }

其次是正常的方法:

  1.  using System;
  2. namespace DecimalToUpper
  3. {
  4.     /*****************************************************************************************  
  5.       *  类名:DecimalToUpper                                                                                   
  6.       *  功能:把金额数据从小写转换为大写                                                           
  7.       *  限制条件:金额小于一万亿,且少于两位小数    
  8.      *  用法:DecimalToUpper  x  =  new  DecimalToUpper();                                  
  9.       *              this.textBox2.Text  =  x.ToUpper(d);                                            
  10.                                                                                                                                         
  11.                                                                                                                                   
  12.       *****************************************************************************************/
  13.     public class DecimalToUpper
  14.     {
  15.         public DecimalToUpper()
  16.         {
  17.             //  
  18.             //  TODO:  在此处添加构造函数逻辑  
  19.             //  
  20.         }
  21.         ///  <summary>  
  22.         ///  小写金额转换为大写金额,其他条件:金额小于一万亿,最多两位小数  
  23.         ///  </summary>  
  24.         ///  <param  name="d">源金额,d  《  1000000000000.00(一万亿),且最多两位小数  </param>  
  25.         ///  <returns>结果,大写金额 </returns>  
  26.         public string ToUpper(decimal d)
  27.         {
  28.             if (d == 0)
  29.                 return "零元整";
  30.             string je = d.ToString("####.00");
  31.             if (je.Length > 15)
  32.                 return "";
  33.             je = new String('0', 15 - je.Length) + je; //若小于15位长,前面补0  
  34.             string stry = je.Substring(0, 4); //取得'亿'单元  
  35.             string strw = je.Substring(4, 4); //取得'万'单元  
  36.             string strg = je.Substring(8, 4); //取得'元'单元  
  37.             string strf = je.Substring(13, 2); //取得小数部分  
  38.             string str1 = "", str2 = "", str3 = "";
  39.             str1 = this.getupper(stry, "亿"); //亿单元的大写  
  40.             str2 = this.getupper(strw, "万"); //万单元的大写  
  41.             str3 = this.getupper(strg, "元"); //元单元的大写  
  42.             string str_y = "", str_w = "";
  43.             if (je[3] == '0' || je[4] == '0'//亿和万之间是否有0  
  44.                 str_y = "零";
  45.             if (je[7] == '0' || je[8] == '0'//万和元之间是否有0  
  46.                 str_w = "零";
  47.             string ret = str1 + str_y + str2 + str_w + str3; //亿,万,元的三个大写合并  
  48.             for (int i = 0; i < ret.Length; i++) //去掉前面的"零"  
  49.             {
  50.                 if (ret[i] != '零')
  51.                 {
  52.                     ret = ret.Substring(i);
  53.                     break;
  54.                 }
  55.             }
  56.             for (int i = ret.Length - 1; i > -1; i--) //去掉最后的"零"  
  57.             {
  58.                 if (ret[i] != '零')
  59.                 {
  60.                     ret = ret.Substring(0, i + 1);
  61.                     break;
  62.                 }
  63.             }
  64.             if (ret[ret.Length - 1] != '元'//若最后不位不是'元',则加一个'元'字  
  65.                 ret = ret + "元";
  66.             if (ret == "零零元"//若为零元,则去掉"元数",结果只要小数部分  
  67.                 ret = "";
  68.             if (strf == "00"//下面是小数部分的转换  
  69.             {
  70.                 ret = ret + "整";
  71.             }
  72.             else
  73.             {
  74.                 string tmp = "";
  75.                 tmp = this.getint(strf[0]);
  76.                 if (tmp == "零")
  77.                     ret = ret + tmp;
  78.                 else
  79.                     ret = ret + tmp + "角";
  80.                 tmp = this.getint(strf[1]);
  81.                 if (tmp == "零")
  82.                     ret = ret + "整";
  83.                 else
  84.                     ret = ret + tmp + "分";
  85.             }
  86.             if (ret[0] == '零')
  87.             {
  88.                 ret = ret.Substring(1); //防止0.03转为"零叁分",而直接转为"叁分"  
  89.             }
  90.             return ret; //完成,返回  
  91.         }  ///  <summary>  
  92.         ///  把一个单元转为大写,如亿单元,万单元,个单元  
  93.         ///  </summary>  
  94.         ///  <param  name="str">这个单元的小写数字(4位长,若不足,则前面补零) </param>  
  95.         ///  <param  name="strDW">亿,万,元 </param>  
  96.         ///  <returns>转换结果 </returns>  
  97.         private string getupper(string str, string strDW)
  98.         {
  99.             if (str == "0000")
  100.                 return "";
  101.             string ret = "";
  102.             string tmp1 = this.getint(str[0]);
  103.             string tmp2 = this.getint(str[1]);
  104.             string tmp3 = this.getint(str[2]);
  105.             string tmp4 = this.getint(str[3]);
  106.             if (tmp1 != "零")
  107.             {
  108.                 ret = ret + tmp1 + "仟";
  109.             }
  110.             else
  111.             {
  112.                 ret = ret + tmp1;
  113.             }
  114.             if (tmp2 != "零")
  115.             {
  116.                 ret = ret + tmp2 + "佰";
  117.             }
  118.             else
  119.             {
  120.                 if (tmp1 != "零"//保证若有两个零'00',结果只有一个零,下同  
  121.                     ret = ret + tmp2;
  122.             }
  123.             if (tmp3 != "零")
  124.             {
  125.                 ret = ret + tmp3 + "拾";
  126.             }
  127.             else
  128.             {
  129.                 if (tmp2 != "零")
  130.                     ret = ret + tmp3;
  131.             }
  132.             if (tmp4 != "零")
  133.             {
  134.                 ret = ret + tmp4;
  135.             }
  136.             if (ret[0] == '零'//若第一个字符是'零',则去掉  
  137.                 ret = ret.Substring(1);
  138.             if (ret[ret.Length - 1] == '零'//若最后一个字符是'零',则去掉  
  139.                 ret = ret.Substring(0, ret.Length - 1);
  140.             return ret + strDW; //加上本单元的单位  
  141.         }
  142.         ///  <summary>  
  143.         ///  单个数字转为大写  
  144.         ///  </summary>  
  145.         ///  <param  name="c">小写阿拉伯数字  0---9 </param>  
  146.         ///  <returns>大写数字 </returns>  
  147.         private string getint(char c)
  148.         {
  149.             string str = "";
  150.             switch (c)
  151.             {
  152.                 case '0':
  153.                     str = "零";
  154.                     break;
  155.                 case '1':
  156.                     str = "壹";
  157.                     break;
  158.                 case '2':
  159.                     str = "贰";
  160.                     break;
  161.                 case '3':
  162.                     str = "叁";
  163.                     break;
  164.                 case '4':
  165.                     str = "肆";
  166.                     break;
  167.                 case '5':
  168.                     str = "伍";
  169.                     break;
  170.                 case '6':
  171.                     str = "陆";
  172.                     break;
  173.                 case '7':
  174.                     str = "柒";
  175.                     break;
  176.                 case '8':
  177.                     str = "拐";
  178.                     break;
  179.                 case '9':
  180.                     str = "玖";
  181.                     break;
  182.             }
  183.             return str;
  184.         }
  185.     }
  186. }