1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
* 生成中文描述的数字串 * @param originStr * @return */ private static String generateChinese(String originStr) { String pointStr = ""; if (originStr.contains(".")) { pointStr = originStr.substring(originStr.indexOf('.') + 1); originStr = originStr.substring(0, originStr.indexOf('.')); } originStr = filterStartWithZero(originStr); StringBuilder sb = new StringBuilder(); for (int i = 0; i < originStr.length(); ++i) { if (i % 4 == 0) { if (i / 4 == 1) { sb.append("万"); } else if (i / 4 == 2) { sb.append("亿"); } } else { if (i % 4 == 1) { sb.append("十"); } else if (i % 4 == 2) { sb.append("百"); } else if (i % 4 == 3) { sb.append("千"); } } sb.append(getChinese(originStr.charAt(originStr.length()-i-1))); } String result = reverse(sb.toString()); result = filterAfterGenerate(result);
StringBuilder pointSB = new StringBuilder(); if (!"".equals(pointStr)) { for (int i = 0; i < pointStr.length(); ++i) { pointSB.append(getChinese(pointStr.charAt(i))); } result = result + "点" + pointSB.toString(); } return result; }
* 最后产生结果后进行过滤,把不相关的数据替换掉 * @param originStr * @return */ private static String filterAfterGenerate(String originStr) { String[] filterData = {"零亿", "零万", "零千", "零百", "零十", "零零"}; String[] replaceData = {"亿", "万", "零", "零", "零", "零"}; int flag = 0; while (true) { for (int i = 0; i < filterData.length; ++i) { if (!originStr.contains(filterData[i])) { flag++; } else { flag = 0; originStr = originStr.replace(filterData[i], replaceData[i]); } } if (flag == filterData.length) { break; } else { flag = 0; } } while (originStr.endsWith("零")) { originStr = originStr.substring(0, originStr.length()-1); } return originStr.equals("") ? "零" : originStr; }
* 根据传入的数字得到中文 * @param num * @return */ private static char getChinese(char num) { char[] digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; char[] chinese = {'零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'}; for (int i = 0; i < digit.length; ++i) { if (digit[i] == num) { return chinese[i]; } } return '零'; }
* 反转字符串 * @param str * @return */ private static String reverse(String str) { StringBuilder sb = new StringBuilder(); for (int i = str.length() - 1; i >= 0; --i) { sb.append(str.charAt(i)); } return sb.toString(); }
* 去除输入字符串开头的零,如:0023输出为23 * @param originStr * @return */ private static String filterStartWithZero(String originStr) { int i = 0; for (; i < originStr.length(); ++i) { if (originStr.charAt(i) != '0') { break; } } return originStr.substring(i); }
public static void main(String[] args) { System.out.println(generateChinese("100010220.2")); System.out.println(generateChinese("12003.2341")); System.out.println(generateChinese("0000.00")); System.out.println(generateChinese("00001.230")); System.out.println(generateChinese("12003943490.12")); }
|