金额大小写转换
刚开通博客也不知道写什么,看博客太空了,就把一个项目中金额大小写转换的类抽出来。废话不说上图上代码。
先看个demo
目前只能精确到 分
主要代码如下
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 namespace 大小写转换 7 { 8 public class ConvertHelper 9 { 10 /// <summary> 11 /// 将小写数字转成大写金额 12 /// </summary> 13 /// <param name="str"></param> 14 /// <returns></returns> 15 public string ConvertNum(string str) 16 { 17 string rstr = ""; 18 if (!IsPositveDecimal(str)) 19 { 20 rstr = "负"; 21 str = str.Substring(1); 22 } 23 //return "输入的不是正数字!"; 24 if (Double.Parse(str) > 999999999999.99) 25 return "数字太大,无法换算,请输入一万亿元以下的金额"; 26 char[] ch = new char[1]; 27 ch[0] = '.'; //小数点 28 string[] splitstr = null; //定义按小数点分割后的字符串数组 29 splitstr = str.Split(ch[0]); //按小数点分割字符串 30 if (splitstr.Length == 1) //只有整数部分 31 return ConvertData(str) + "圆整"; 32 else //有小数部分 33 { 34 35 rstr += ConvertData(splitstr[0]) + "圆";//转换整数部分 36 rstr += ConvertXiaoShu(splitstr[1]); //转换小数部分 37 return rstr; 38 } 39 } 40 41 42 /// 43 /// 判断是否是正数字字符串 44 /// 45 /// 判断字符串 46 /// 如果是数字,返回true,否则返回false 47 public bool IsPositveDecimal(string str) 48 { 49 Decimal d; 50 try 51 { 52 d = Decimal.Parse(str); 53 } 54 catch (Exception) 55 { 56 return false; 57 } 58 if (d >= 0) 59 return true; 60 else 61 return false; 62 } 63 64 65 /// 66 /// 转换数字(整数) 67 /// 68 /// 需要转换的整数数字字符串 69 /// 转换成中文大写后的字符串 70 public string ConvertData(string str) 71 { 72 string tmpstr = ""; 73 string rstr = ""; 74 int strlen = str.Length; 75 if (strlen <= 4)//数字长度小于四位 76 { 77 rstr = ConvertDigit(str); 78 79 } 80 else 81 { 82 if (strlen <= 8)//数字长度大于四位,小于八位 83 { 84 tmpstr = str.Substring(strlen - 4, 4); //先截取最后四位数字 85 rstr = ConvertDigit(tmpstr);//转换最后四位数字 86 tmpstr = str.Substring(0, strlen - 4);//截取其余数字 87 //将两次转换的数字加上万后相连接 88 rstr = String.Concat(ConvertDigit(tmpstr) + "万", rstr); 89 rstr = rstr.Replace("零零", "零"); 90 } 91 else 92 if (strlen <= 12)//数字长度大于八位,小于十二位 93 { 94 tmpstr = str.Substring(strlen - 4, 4); //先截取最后四位数字 95 rstr = ConvertDigit(tmpstr);//转换最后四位数字 96 tmpstr = str.Substring(strlen - 8, 4); //再截取四位数字 97 rstr = String.Concat(ConvertDigit(tmpstr) + "万", rstr); 98 tmpstr = str.Substring(0, strlen - 8); 99 rstr = String.Concat(ConvertDigit(tmpstr) + "亿", rstr); 100 rstr = rstr.Replace("零亿", "亿"); 101 rstr = rstr.Replace("零万", "零"); 102 rstr = rstr.Replace("零零", "零"); 103 rstr = rstr.Replace("零零", "零"); 104 } 105 } 106 strlen = rstr.Length; 107 if (strlen >= 2) 108 { 109 switch (rstr.Substring(strlen - 2, 2)) 110 { 111 case "佰零": rstr = rstr.Substring(0, strlen - 2) + "佰"; break; 112 case "仟零": rstr = rstr.Substring(0, strlen - 2) + "仟"; break; 113 case "万零": rstr = rstr.Substring(0, strlen - 2) + "万"; break; 114 case "亿零": rstr = rstr.Substring(0, strlen - 2) + "亿"; break; 115 } 116 } 117 return rstr; 118 } 119 120 121 122 /// 123 /// 转换数字(小数部分) 124 /// 125 /// 需要转换的小数部分数字字符串 126 /// 转换成中文大写后的字符串 127 public string ConvertXiaoShu(string str) 128 { 129 int strlen = str.Length; 130 string rstr; 131 if (strlen == 1) 132 { 133 rstr = ConvertChinese(str) + "角"; 134 return rstr; 135 } 136 else 137 { 138 string tmpstr = str.Substring(0, 1); 139 rstr = ConvertChinese(tmpstr) + "角"; 140 tmpstr = str.Substring(1, 1); 141 rstr += ConvertChinese(tmpstr) + "分"; 142 rstr = rstr.Replace("零分", ""); 143 rstr = rstr.Replace("零角", ""); 144 return rstr; 145 } 146 } 147 148 149 /// 150 /// 转换数字 151 /// 152 /// 转换的字符串(四位以内) 153 /// 154 public string ConvertDigit(string str) 155 { 156 int strlen = str.Length; 157 string rstr = ""; 158 switch (strlen) 159 { 160 case 1: rstr = ConvertChinese(str); break; 161 case 2: rstr = Convert2Digit(str); break; 162 case 3: rstr = Convert3Digit(str); break; 163 case 4: rstr = Convert4Digit(str); break; 164 } 165 rstr = rstr.Replace("拾零", "拾"); 166 strlen = rstr.Length; 167 return rstr; 168 } 169 170 171 /// 172 /// 转换四位数字 173 /// 174 public string Convert4Digit(string str) 175 { 176 string str1 = str.Substring(0, 1); 177 string str2 = str.Substring(1, 1); 178 string str3 = str.Substring(2, 1); 179 string str4 = str.Substring(3, 1); 180 string rstring = ""; 181 rstring += ConvertChinese(str1) + "仟"; 182 rstring += ConvertChinese(str2) + "佰"; 183 rstring += ConvertChinese(str3) + "拾"; 184 rstring += ConvertChinese(str4); 185 rstring = rstring.Replace("零仟", "零"); 186 rstring = rstring.Replace("零佰", "零"); 187 rstring = rstring.Replace("零拾", "零"); 188 rstring = rstring.Replace("零零", "零"); 189 rstring = rstring.Replace("零零", "零"); 190 rstring = rstring.Replace("零零", "零"); 191 return rstring; 192 } 193 194 /// 195 /// 转换三位数字 196 /// 197 public string Convert3Digit(string str) 198 { 199 string str1 = str.Substring(0, 1); 200 string str2 = str.Substring(1, 1); 201 string str3 = str.Substring(2, 1); 202 string rstring = ""; 203 rstring += ConvertChinese(str1) + "佰"; 204 rstring += ConvertChinese(str2) + "拾"; 205 rstring += ConvertChinese(str3); 206 rstring = rstring.Replace("零佰", "零"); 207 rstring = rstring.Replace("零拾", "零"); 208 rstring = rstring.Replace("零零", "零"); 209 rstring = rstring.Replace("零零", "零"); 210 return rstring; 211 } 212 213 214 /// 215 /// 转换二位数字 216 /// 217 public string Convert2Digit(string str) 218 { 219 string str1 = str.Substring(0, 1); 220 string str2 = str.Substring(1, 1); 221 string rstring = ""; 222 rstring += ConvertChinese(str1) + "拾"; 223 rstring += ConvertChinese(str2); 224 rstring = rstring.Replace("零拾", "零"); 225 rstring = rstring.Replace("零零", "零"); 226 return rstring; 227 } 228 229 230 /// 231 /// 将一位数字转换成中文大写数字 232 /// 233 public string ConvertChinese(string str) 234 { 235 //"零壹贰叁肆伍陆柒捌玖拾佰仟万亿圆整角分" 236 string cstr = ""; 237 switch (str) 238 { 239 case "0": cstr = "零"; break; 240 case "1": cstr = "壹"; break; 241 case "2": cstr = "贰"; break; 242 case "3": cstr = "叁"; break; 243 case "4": cstr = "肆"; break; 244 case "5": cstr = "伍"; break; 245 case "6": cstr = "陆"; break; 246 case "7": cstr = "柒"; break; 247 case "8": cstr = "捌"; break; 248 case "9": cstr = "玖"; break; 249 } 250 return (cstr); 251 } 252 } 253 254 }
直接调用ConvertNum()方法传人金额即可。菜鸟的第一篇文章写完了!!!