货币数值小写转换大写
检测模块:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import org.junit.Test; 2 3 public class IoDetection 4 { 5 private static int a = 0;// 如果为零表示val为数值 6 7 @Test 8 public void ioDetection(String var) 9 { 10 String[] str = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", 11 "9", ".", "0" }; 12 if (null == var) 13 { 14 System.out.println("不可输入空值!"); 15 System.exit(0); 16 } 17 try 18 { 19 String intVar = var.substring(0, 1); 20 // 提取用户录入正数位 21 for (int j = 1; j < var.length(); j++) 22 { 23 if (".".equals(var.substring(j - 1, 24 var.length() - (var.length() - j)))) 25 { 26 break; 27 } 28 else 29 { 30 intVar += var.substring(j, j + 1); 31 } 32 } 33 34 // System.out.println(intVar); 35 36 if (9 < intVar.length()) 37 { 38 System.out.println("用户整数部分位数最多为8位!"); 39 System.exit(0); 40 } 41 42 for (int i = 0; i < var.length(); i++) 43 { 44 // 取var的每個字節 45 int x = i + 1; 46 String strVal = var.toString().substring(i, x); 47 if (0 == a) 48 { 49 // 将var的每個字節与str数组对比 50 for (int j = 0; j < str.length; j++) 51 { 52 a = 1; 53 if (strVal.equals(str[j])) 54 { 55 a = 0; 56 break; 57 } 58 else 59 { 60 a = 1; 61 } 62 } 63 } 64 // System.out.println(strVal + " a=" + a); 65 if (1 == a) 66 { 67 System.out.println("用户输入的字符不都为数字,无法转换"); 68 System.exit(0); 69 } 70 } 71 Convert convert = new Convert(); 72 System.out.println(convert.toConver(var)); 73 } 74 catch (Exception e) 75 { 76 e.printStackTrace(); 77 } 78 } 79 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import org.junit.Test; 2 3 public class Convert 4 { 5 String showVal;// 用于存储数据大写 6 String intVal;// 用于存储整数位大写 7 String floatVal;// 用于存储小数位大写 8 private static String intStr;// 用于存储小数点前面的数 9 private static String floatStr;// 用于存储小数点后面的数 10 private static int pont;// 检测是否有小数位存在 11 String[] varArray = new String[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", 12 "捌", "玖" }; 13 String[] intArray = new String[] { "元", "拾", "佰", "仟", "万", "拾万", "佰万", 14 "千万" }; 15 String[] floatArray = new String[] { "角", "分" }; 16 String integral = "整"; 17 18 @Test 19 public String toConver(String val) 20 { 21 // String val = "0.01"; 22 try 23 { 24 if ("0".equals(val) || "0.0".equals(val) || "0.00".equals(val) 25 || ".00".equals(val)) 26 { 27 showVal = "零元";// 需要return出去 28 return showVal; 29 // System.out.println(showVal); 30 } 31 else 32 { 33 34 // 检测用户输入的数值首位是否有零存在,并将val去首位零值赋给newVal。 35 for (int i = 0; i < val.length(); i++) 36 { 37 if ("0".equals(val.substring(0, 38 val.length() - (val.length() - 1)))) 39 { 40 val = val.substring(1); 41 } 42 else 43 { 44 break; 45 } 46 } 47 String newVal = val; 48 49 // 检测newVal是否有小数点存在,如果存在则pont值不等于零 50 for (int j = 0; j < newVal.length(); j++) 51 { 52 int i = j + 1; 53 if (".".equals(newVal.substring(j, i))) 54 { 55 pont = j + 1;// int类型默认值为零 56 break; 57 } 58 } 59 // 如果pont不等于零,说明存在小数位则区分整数部分和小数部分反之整数部分intStr = newVal 60 if (0 != pont) 61 { 62 // 如果newVal值首位为小数点则给floatStr赋值反之给intStr和floatStr赋值 63 if (".".equals(newVal.substring(0, 1))) 64 { 65 intStr = null; 66 floatStr = newVal.substring(1, newVal.length()); 67 } 68 else 69 { 70 String tem = newVal.substring(pont); 71 if (2 != tem.length()) 72 { 73 newVal += "0"; 74 } 75 intStr = newVal.substring(0, pont - 1); 76 floatStr = newVal.substring(pont, pont + 2); 77 } 78 } 79 else 80 { 81 intStr = newVal; 82 } 83 84 } 85 86 // 大写转换 87 // 如果只存在小数位置时 88 if (null == floatStr) 89 { 90 floatVal = integral; 91 } 92 else if (null != floatStr) 93 { 94 if ("0".equals(floatStr) || "00".equals(floatStr)) 95 { 96 floatVal = integral; 97 } 98 99 else 100 { 101 if ("0".equals(floatStr.substring(0, 1)) 102 && floatStr.length() > 1) 103 { 104 floatVal = "零角"; 105 floatVal += varArray[Integer.parseInt(floatStr 106 .substring(1, 2))]; 107 floatVal += floatArray[1]; 108 } 109 else 110 { 111 // 取小数位数字符并转为Int型从而获取varArray中的大写数值 112 floatVal = varArray[Integer.parseInt(floatStr 113 .substring(0, 1))]; 114 // 获限floatArray中的角 115 floatVal += floatArray[0]; 116 for (int i = 1; i < floatStr.length(); i++) 117 { 118 // 获得分位上的大写数值 119 int j = i + 1; 120 String var = floatStr.substring(i, j).toString(); 121 int endIdx = Integer.parseInt(var); 122 floatVal += varArray[endIdx]; 123 // 获得floatArray中的分 124 floatVal += floatArray[1]; 125 } 126 127 } 128 } 129 } 130 131 // 取整数部分 132 if (null != intStr) 133 { 134 135 // 取整数位数字符并转为Int型从而获取varArray中的大写数值 136 intVal = varArray[Integer.parseInt(intStr.substring(0, 1))]; 137 // 获限floatArray中的角 138 intVal += intArray[(intStr.length() - 1)]; 139 if (Integer.parseInt("1") < intStr.length()) 140 { 141 for (int i = 1; i < intStr.length(); i++) 142 { 143 int j = intStr.length() - (intStr.length() - (i + 1)); 144 if ("0".equals(intStr.substring(i, intStr.length() 145 - (intStr.length() - (i + 1))))) 146 { 147 intVal += "零"; 148 } 149 else 150 { 151 // 取整数位数字符并转为Int型从而获取varArray中的大写数值 152 intVal += varArray[Integer.parseInt(intStr 153 .substring(i, j))]; 154 // 获限floatArray中的角 155 intVal += intArray[(intStr.length() - j)]; 156 } 157 } 158 } 159 } 160 // 检测intVal整数部分是否末尾为“零”,若为“零”则清除 161 String temIntVal; 162 // System.out.println(intVal); 163 if (null != intVal) 164 { 165 for (int i = 0; i < intVal.length(); i++) 166 { 167 if ("零".equals(intVal.substring(intVal.length() - 1, 168 intVal.length()))) 169 { 170 temIntVal = intVal.substring(0, intVal.length() - 1); 171 intVal = temIntVal;// } 172 } 173 } 174 } 175 if (null != intVal) 176 { 177 if ("元".equals(intVal.substring(intVal.length() - 1, 178 intVal.length()))) 179 { 180 } 181 else 182 { 183 intVal += "元"; 184 } 185 // 检测是否重复出现“零”,若重复则保留一个“零” 186 String temIntVal2 = null; 187 for (int i = 0; i < intVal.length() - 1; i++) 188 { 189 String str1 = intVal.substring(i, 190 intVal.length() - (intVal.length() - (i + 1))); 191 int j = i + 1; 192 String str2 = intVal.substring(j, 193 intVal.length() - (intVal.length() - (i + 2))); 194 if (str1.equals(str2)) 195 { 196 197 String tep1 = intVal.substring(0, i); 198 String tep2 = intVal.substring(j + 1); 199 for (int x = 1; x < tep2.length(); x++) 200 { 201 if ("零".equals(tep2.subSequence(0, tep2.length() 202 - (tep2.length() - x)))) 203 { 204 tep2 = tep2.substring(1); 205 } 206 } 207 temIntVal2 = tep1 + tep2; 208 intVal = temIntVal2; 209 } 210 } 211 } 212 // 检测重复“万”保留最后一个 213 int num = 0; 214 if (null != intVal) 215 { 216 for (int i = 0; i < intVal.length(); i++) 217 { 218 if ("万".equals(intVal.substring(i, intVal.length() 219 - (intVal.length() - (i + 1))))) 220 { 221 num = num + 1; 222 } 223 } 224 String str1 = null; 225 String str2 = null; 226 String str3 = intVal; 227 if (num > 1) 228 { 229 for (int i = 1; i < num; i++) 230 { 231 for (int j = 0; j < str3.length(); j++) 232 { 233 if ("万".equals(str3.substring(j, str3.length() 234 - (str3.length() - (j + 1))))) 235 { 236 str1 = str3.substring(0, j); 237 str2 = str3.substring(j + 1, str3.length()); 238 str3 = str1 + str2; 239 break; 240 } 241 } 242 } 243 } 244 intVal = str3; 245 } 246 // 判断intVal整数部分和floatVal小数部分,并将丰空值组合后赋给showVal 247 showVal = "货币数值大写:"; 248 if (null != intVal && null != floatVal) 249 { 250 showVal += intVal + floatVal; 251 } 252 else if (null != intVal) 253 { 254 showVal += intVal; 255 } 256 else if (null != floatVal) 257 { 258 showVal += floatVal; 259 } 260 } 261 catch (Exception e) 262 { 263 e.printStackTrace(); 264 } 265 return showVal; 266 } 267 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Test 2 { 3 4 public static void main(String[] args) 5 { 6 IoDetection ioDetection = new IoDetection(); 7 ioDetection.ioDetection("25975428"); 8 } 9 }