人民币大小写转换

View Code
  1 using System;
  2 using System.Data;
  3 using System.Configuration;
  4 using System.Web;
  5 using System.Web.Security;
  6 using System.Web.UI;
  7 using System.Web.UI.HtmlControls;
  8 using System.Web.UI.WebControls;
  9 using System.Web.UI.WebControls.WebParts;
 10 
 11 /// <summary>
 12 ///EcanRMB 的摘要说明
 13 /// </summary>
 14 public class EcanRMB
 15 {
 16     public EcanRMB()
 17     {
 18         //
 19         //TODO: 在此处添加构造函数逻辑
 20         //
 21     }
 22     /// <summary>
 23     /// 转换人民币大小金额
 24     /// </summary>
 25     /// <param name="num">金额</param>
 26     /// <returns>返回大写形式</returns>
 27     public string CmycurD(decimal num)
 28     {
 29         string str1 = "零壹贰叁肆伍陆柒捌玖"//0-9所对应的汉字
 30         string str2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"//数字位所对应的汉字
 31         string str3 = ""//从原num值中取出的值
 32         string str4 = ""//数字的字符串形式
 33         string str5 = ""//人民币大写金额形式
 34         int i; //循环变量
 35         int j; //num的值乘以100的字符串长度
 36         string ch1 = ""//数字的汉语读法
 37         string ch2 = ""//数字位的汉字读法
 38         int nzero = 0//用来计算连续的零值是几个
 39         int temp; //从原num值中取出的值
 40 
 41         num = Math.Round(Math.Abs(num), 2); //将num取绝对值并四舍五入取2位小数
 42         str4 = ((long)(num * 100)).ToString(); //将num乘100并转换成字符串形式
 43         j = str4.Length; //找出最高位
 44         if (j > 15) { return "溢出"; }
 45         str2 = str2.Substring(15 - j); //取出对应位数的str2的值。如:200.55,j为5所以str2=佰拾元角分
 46 
 47         //循环取出每一位需要转换的值
 48         for (i = 0; i < j; i++)
 49         {
 50             str3 = str4.Substring(i, 1); //取出需转换的某一位的值
 51             temp = Convert.ToInt32(str3); //转换为数字
 52             if (i != (j - 3) && i != (j - 7) && i != (j - 11) && i != (j - 15))
 53             {
 54                 //当所取位数不为元、万、亿、万亿上的数字时
 55                 if (str3 == "0")
 56                 {
 57                     ch1 = "";
 58                     ch2 = "";
 59                     nzero = nzero + 1;
 60                 }
 61                 else
 62                 {
 63                     if (str3 != "0" && nzero != 0)
 64                     {
 65                         ch1 = "" + str1.Substring(temp * 11);
 66                         ch2 = str2.Substring(i, 1);
 67                         nzero = 0;
 68                     }
 69                     else
 70                     {
 71                         ch1 = str1.Substring(temp * 11);
 72                         ch2 = str2.Substring(i, 1);
 73                         nzero = 0;
 74                     }
 75                 }
 76             }
 77             else
 78             {
 79                 //该位是万亿,亿,万,元位等关键位
 80                 if (str3 != "0" && nzero != 0)
 81                 {
 82                     ch1 = "" + str1.Substring(temp * 11);
 83                     ch2 = str2.Substring(i, 1);
 84                     nzero = 0;
 85                 }
 86                 else
 87                 {
 88                     if (str3 != "0" && nzero == 0)
 89                     {
 90                         ch1 = str1.Substring(temp * 11);
 91                         ch2 = str2.Substring(i, 1);
 92                         nzero = 0;
 93                     }
 94                     else
 95                     {
 96                         if (str3 == "0" && nzero >= 3)
 97                         {
 98                             ch1 = "";
 99                             ch2 = "";
100                             nzero = nzero + 1;
101                         }
102                         else
103                         {
104                             if (j >= 11)
105                             {
106                                 ch1 = "";
107                                 nzero = nzero + 1;
108                             }
109                             else
110                             {
111                                 ch1 = "";
112                                 ch2 = str2.Substring(i, 1);
113                                 nzero = nzero + 1;
114                             }
115                         }
116                     }
117                 }
118             }
119             if (i == (j - 11) || i == (j - 3))
120             {
121                 //如果该位是亿位或元位,则必须写上
122                 ch2 = str2.Substring(i, 1);
123             }
124             str5 = str5 + ch1 + ch2;
125 
126             if (i == j - 1 && str3 == "0")
127             {
128                 //最后一位(分)为0时,加上"整"
129                 str5 = str5 + '';
130             }
131         }
132         if (num == 0)
133         {
134             str5 = "零元整";
135         }
136         return str5;
137     }
138 
139     /**/
140     /// <summary>
141     /// 一个重载,将字符串先转换成数字在调用CmycurD(decimal num)
142     /// </summary>
143     /// <param name="num">用户输入的金额,字符串形式未转成decimal</param>
144     /// <returns></returns>
145     public string CmycurD(string numstr)
146     {
147         try
148         {
149             decimal num = Convert.ToDecimal(numstr);
150             return CmycurD(num);
151         }
152         catch
153         {
154             return "非数字形式!";
155         }
156     }
157 
158 }
下载:通用人民币转换类
posted @ 2012-05-02 16:18  双魂人生  阅读(494)  评论(0编辑  收藏  举报