数字转换中文
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class NumTotring {
public static void main(String[] args) {
String a[] = { "129830200000", "12983020000", "1298302000",
"129830200", "23000042", "2000042", "100345", "23864", "1000",
"1234", "123", "23", "5", "20000000", "10086", "10010" };
for (int i = 0; i < a.length; i++) {
System.out.println("原数字为:" + a[i] + "切换后的读法为:" + readChinese(Long.parseLong(a[i])));
}
}
private static Map<Integer, String> numberStringMap = new HashMap<Integer, String>();
private static Map<Integer, String> base10StringMap = new HashMap<Integer, String>();
static {
numberStringMap.put(0, "零");
numberStringMap.put(1, "壹");
numberStringMap.put(2, "贰");
numberStringMap.put(3, "叁");
numberStringMap.put(4, "肆");
numberStringMap.put(5, "伍");
numberStringMap.put(6, "陆");
numberStringMap.put(7, "柒");
numberStringMap.put(8, "捌");
numberStringMap.put(9, "玖");
base10StringMap.put(1, "");
base10StringMap.put(2, "十");
base10StringMap.put(3, "百");
base10StringMap.put(4, "千");
base10StringMap.put(5, "万");
}
private static String readChinese(long a) {
String intString = a + "";
char ichar[] = intString.toCharArray();
char icharrevert[] = revertCharArray(ichar);
int i = 0;
String result = "";
while (i < icharrevert.length) {
int j=i + 4 < icharrevert.length ? i + 4 : icharrevert.length;
char[] icharsplit = Arrays.copyOfRange(icharrevert, i, j);
result = processSplitedNumber(icharsplit, (i + 1) / 4) + result;
i = i + 4;
}
return result;
}
private static char[] revertCharArray(char[] ichar) {
char icharNew[] = new char[ichar.length];
for (int i = ichar.length - 1, j = 0; i >= 0 && j < ichar.length; i--, j++) {
icharNew[j] = ichar[i];
}
return icharNew;
}
private static String processSplitedNumber(char[] num, int time) {
StringBuffer sb = new StringBuffer();
for (int i = num.length - 1; i >= 0; i--) {
if (num[i] == '0') {
if (i == 0) {
continue;
}
if (num[i - 1] != '0') {
sb.append(numberStringMap.get(Integer.valueOf(num[i] + "")));
} else {
continue;
}
} else {
sb.append(numberStringMap.get(Integer.valueOf(num[i] + "")));
sb.append(base10StringMap.get(i + 1));
}
}
if (time == 0) {
} else if (time == 1) {
sb.append("万");
} else if (time == 2) {
sb.append("亿");
}
return sb.toString();
}
}
原数字为:129830200000切换后的读法为:壹千贰百玖十捌亿叁千零贰十万
原数字为:12983020000切换后的读法为:壹百贰十玖亿捌千叁百零贰万
原数字为:1298302000切换后的读法为:壹十贰亿玖千捌百叁十万贰千
原数字为:129830200切换后的读法为:壹亿贰千玖百捌十叁万零贰百
原数字为:23000042切换后的读法为:贰千叁百万零肆十贰
原数字为:2000042切换后的读法为:贰百万零肆十贰
原数字为:100345切换后的读法为:壹十万零叁百肆十伍
原数字为:23864切换后的读法为:贰万叁千捌百陆十肆
原数字为:1000切换后的读法为:壹千
原数字为:1234切换后的读法为:壹千贰百叁十肆
原数字为:123切换后的读法为:壹百贰十叁
原数字为:23切换后的读法为:贰十叁
原数字为:5切换后的读法为:伍
原数字为:20000000切换后的读法为:贰千万
原数字为:10086切换后的读法为:壹万零捌十陆
原数字为:10010切换后的读法为:壹万零壹十