public static void main(String[] args) throws IOException {
int num = 2100500001;
System.out.println(convert(num));
} private static boolean need = false; public static String convert(int num) {
need = false;
String str = num + "";
char[] chars = str.toCharArray();
String result = "";
int index = 0;
boolean hasZero = false;
boolean theFirst = false;
for (int i = chars.length - 1; i > -1; i--) {
index++; if (index % 4 == 1 && index > 4) {
need = true;
} if (chars[i] == '0') {
hasZero = true;
if (index == 1) {
theFirst = true;
}
continue;
} else if (hasZero) {
hasZero = false;
if (theFirst) {
theFirst = false;
} else {
result = "零" + result;
}
}
if (index % 4 == 2) {
result = getChineseNum(chars[i]) + "拾" + getInfour(index)
+ result;
} else if (index % 4 == 3) {
result = getChineseNum(chars[i]) + "佰" + getInfour(index)
+ result;
} else if (index % 4 == 0) {
result = getChineseNum(chars[i]) + "仟" + getInfour(index)
+ result;
} else if (index % 4 == 1) {
result = getChineseNum(chars[i]) + getInfour(index) + result;
}
}
return result;
} private static String getInfour(int index) {
if (need) {
int a = index / 4;
switch (a) {
case 1:
need = false;
return "万";
case 2:
need = false;
return "亿";
}
}
return "";
} private static char getChineseNum(char num) {
switch (num) {
case '1':
return '壹';
case '2':
return '贰'; case '3':
return '叁'; case '4':
return '肆'; case '5':
return '伍'; case '6':
return '陆'; case '7':
return '柒'; case '8':
return '捌'; case '9':
return '玖';
}
return ' ';
} }