转换人民币大小金额

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