1 public static class Money { 2 private static readonly String cnNumber = "零壹贰叁肆伍陆柒捌玖"; 3 private static readonly String cnUnit = "分角元拾佰仟万拾佰仟亿拾佰仟兆拾佰仟"; 4 5 private static readonly String[] enSmallNumber = { "", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN" }; 6 private static readonly String[] enLargeNumber = { "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY" }; 7 private static readonly String[] enUnit = { "", "THOUSAND", "MILLION", "BILLION", "TRILLION" }; 8 9 10 // 以下是货币金额中文大写转换方法 11 public static String GetCnString(String MoneyString) { 12 String[] tmpString = MoneyString.Split('.'); 13 String intString = MoneyString; // 默认为整数 14 String decString = ""; // 保存小数部分字串 15 String rmbCapital = ""; // 保存中文大写字串 16 int k; 17 int j; 18 int n; 19 20 if (tmpString.Length > 1) { 21 intString = tmpString[0]; // 取整数部分 22 decString = tmpString[1]; // 取小数部分 23 } 24 decString += "00"; 25 decString = decString.Substring(0, 2); // 保留两位小数位 26 intString += decString; 27 28 try { 29 k = intString.Length - 1; 30 if (k > 0 && k < 18) { 31 for (int i = 0; i <= k; i++) { 32 j = (int)intString[i] - 48; 33 // rmbCapital = rmbCapital + cnNumber[j] + cnUnit[k-i]; // 供调试用的直接转换 34 n = i + 1 >= k ? (int)intString[k] - 48 : (int)intString[i + 1] - 48; // 等效于 if( ){ }else{ } 35 if (j == 0) { 36 if (k - i == 2 || k - i == 6 || k - i == 10 || k - i == 14) { 37 rmbCapital += cnUnit[k - i]; 38 } 39 else { 40 if (n != 0) { 41 rmbCapital += cnNumber[j]; 42 } 43 } 44 } 45 else { 46 rmbCapital = rmbCapital + cnNumber[j] + cnUnit[k - i]; 47 } 48 } 49 50 rmbCapital = rmbCapital.Replace("兆亿万", "兆"); 51 rmbCapital = rmbCapital.Replace("兆亿", "兆"); 52 rmbCapital = rmbCapital.Replace("亿万", "亿"); 53 rmbCapital = rmbCapital.TrimStart('元'); 54 rmbCapital = rmbCapital.TrimStart('零'); 55 56 return rmbCapital; 57 } 58 else { 59 return ""; // 超出转换范围时,返回零长字串 60 } 61 } 62 catch { 63 return ""; // 含有非数值字符时,返回零长字串 64 } 65 } 66 67 // 以下是货币金额英文大写转换方法 68 public static String GetEnString(String MoneyString) { 69 String[] tmpString = MoneyString.Split('.'); 70 String intString = MoneyString; // 默认为整数 71 String decString = ""; // 保存小数部分字串 72 String engCapital = ""; // 保存英文大写字串 73 String strBuff1; 74 String strBuff2; 75 String strBuff3; 76 int curPoint; 77 int i1; 78 int i2; 79 int i3; 80 int k; 81 int n; 82 83 if (tmpString.Length > 1) { 84 intString = tmpString[0]; // 取整数部分 85 decString = tmpString[1]; // 取小数部分 86 } 87 decString += "00"; 88 decString = decString.Substring(0, 2); // 保留两位小数位 89 90 try { 91 // 以下处理整数部分 92 curPoint = intString.Length - 1; 93 if (curPoint >= 0 && curPoint < 15) { 94 k = 0; 95 while (curPoint >= 0) { 96 strBuff1 = ""; 97 strBuff2 = ""; 98 strBuff3 = ""; 99 if (curPoint >= 2) { 100 n = int.Parse(intString.Substring(curPoint - 2, 3)); 101 if (n != 0) { 102 i1 = n / 100; // 取佰位数值 103 i2 = (n - i1 * 100) / 10; // 取拾位数值 104 i3 = n - i1 * 100 - i2 * 10; // 取个位数值 105 if (i1 != 0) { 106 strBuff1 = enSmallNumber[i1] + " HUNDRED "; 107 } 108 if (i2 != 0) { 109 if (i2 == 1) { 110 strBuff2 = enSmallNumber[i2 * 10 + i3] + " "; 111 } 112 else { 113 strBuff2 = enLargeNumber[i2 - 2] + " "; 114 if (i3 != 0) { 115 strBuff3 = enSmallNumber[i3] + " "; 116 } 117 } 118 } 119 else { 120 if (i3 != 0) { 121 strBuff3 = enSmallNumber[i3] + " "; 122 } 123 } 124 engCapital = strBuff1 + strBuff2 + strBuff3 + enUnit[k] + " " + engCapital; 125 } 126 } 127 else { 128 n = int.Parse(intString.Substring(0, curPoint + 1)); 129 if (n != 0) { 130 i2 = n / 10; // 取拾位数值 131 i3 = n - i2 * 10; // 取个位数值 132 if (i2 != 0) { 133 if (i2 == 1) { 134 strBuff2 = enSmallNumber[i2 * 10 + i3] + " "; 135 } 136 else { 137 strBuff2 = enLargeNumber[i2 - 2] + " "; 138 if (i3 != 0) { 139 strBuff3 = enSmallNumber[i3] + " "; 140 } 141 } 142 } 143 else { 144 if (i3 != 0) { 145 strBuff3 = enSmallNumber[i3] + " "; 146 } 147 } 148 engCapital = strBuff2 + strBuff3 + enUnit[k] + " " + engCapital; 149 } 150 } 151 152 ++k; 153 curPoint -= 3; 154 } 155 engCapital = engCapital.TrimEnd(); 156 } 157 158 159 // 以下处理小数部分 160 strBuff2 = ""; 161 strBuff3 = ""; 162 n = int.Parse(decString); 163 if (n != 0) { 164 i2 = n / 10; // 取拾位数值 165 i3 = n - i2 * 10; // 取个位数值 166 if (i2 != 0) { 167 if (i2 == 1) { 168 strBuff2 = enSmallNumber[i2 * 10 + i3] + " "; 169 } 170 else { 171 strBuff2 = enLargeNumber[i2 - 2] + " "; 172 if (i3 != 0) { 173 strBuff3 = enSmallNumber[i3] + " "; 174 } 175 } 176 } 177 else { 178 if (i3 != 0) { 179 strBuff3 = enSmallNumber[i3] + " "; 180 } 181 } 182 183 // 将小数字串追加到整数字串后 184 if (engCapital.Length > 0) { 185 engCapital = engCapital + " AND CENTS " + strBuff2 + strBuff3; // 有整数部分时 186 } 187 else { 188 engCapital = "CENTS " + strBuff2 + strBuff3; // 只有小数部分时 189 } 190 } 191 192 engCapital = engCapital.TrimEnd(); 193 return engCapital; 194 } 195 catch { 196 return ""; // 含非数字字符时,返回零长字串 197 } 198 } 199 }
转发链接:https://www.likecs.com/show-308585559.html
标签:String,engCapital,rmbCapital,C#,i3,i2,大写,转换方法,int From: https://www.cnblogs.com/guangfangxiaokeai/p/17163321.html