/**
* 10、 金额转换,阿拉伯数字转换成中国传统形式。 例如:101000001010 转换为 壹仟零壹拾亿零壹仟零壹拾圆整
*
* @author:Jwd
*
*/
public class Exam10 {
public static void main(String[] args) throws IOException {
System.out.println("请从键盘上随便输入一个金额");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
System.out.println(numToWorda(s).toString().replaceAll("零[拾佰仟]","零").replaceAll("零+亿","亿").replaceAll("零{3}万","零").
replaceAll("零+万","万").replaceAll("零+圆","圆").replaceAll("零+","零"));
}
public static StringBuffer numToWord(String num) {
char[] arr = num.toCharArray();
StringBuffer result = new StringBuffer("圆整");
Map<String, String> m = new HashMap<String, String>();
Long no = Long.parseLong(num);
Long nr = no;
String key = "";
String value = "";
for (int j = arr.length; j > 0; j--) {
key = Integer.toString(10 + j);
if (j != 8 && j != 12) {
switch (j % 4) {
case 1:
value = "拾";
break;
case 2:
value = "佰";
break;
case 3:
value = "仟";
break;
case 0:
value = "万";
break;
}
} else if (j == 8) {
value = "亿";
}
m.put(key, value);
}
m.put("0", "零");
m.put("1", "壹");
m.put("2", "贰");
m.put("3", "叁");
m.put("4", "肆");
m.put("5", "伍");
m.put("6", "陆");
m.put("7", "柒");
m.put("8", "捌");
m.put("9", "玖");
String sz = "";// 当前值
if (nr != 0) {
for (int i = 0; i < arr.length; i++) {
// 除10取余
nr = no % 10;//nr初始值为输入值,后为取余值,即当前值的阿拉伯形式。。
no = no / 10;
sz = m.get(nr.toString());// 获取值
if (nr == 0 && i != 8 && i != 4) {// i为零时从MAP中找不到对应的单位.
System.out.println("=======000000000");
// result = result.insert(0, m.get(Integer.toString(10 +
// i)));// 获取单位
result = result.insert(0, sz);// 将当前值放在字符串最前面.
} else if (nr != 0) {
System.out.println("!!!!!!!!!!!!!!!!!!!!0");
result = result.insert(0, m.get(Integer.toString(10 + i)));// 获取单位
result = result.insert(0, sz);// 将当前值放在字符串最前面.
} else if (nr == 0 && (i == 8 || i == 4)) {
System.out.println("444444!!!!!!!!!!!!!!!!0");
result = result.insert(0, m.get(Integer.toString(10 + i)));// 获取单位
}
}
} else {
return result.insert(0, "零");
}
return result;
}
public static StringBuffer numToWorda(String num) {
char[] arr = num.toCharArray();
StringBuffer result = new StringBuffer("圆整");
Map<String, String> m = new HashMap<String, String>();
Long no = Long.parseLong(num);
Long nr = no;
String key = "";
String value = "";
for (int j = arr.length; j > 0; j--) {
key = Integer.toString(10 + j);
if (j != 8 && j != 12) {
switch (j % 4) {
case 1:
value = "拾";
break;
case 2:
value = "佰";
break;
case 3:
value = "仟";
break;
case 0:
value = "万";
break;
}
} else if (j == 8) {
value = "亿";
}
m.put(key, value);
}
m.put("0", "零");
m.put("1", "壹");
m.put("2", "贰");
m.put("3", "叁");
m.put("4", "肆");
m.put("5", "伍");
m.put("6", "陆");
m.put("7", "柒");
m.put("8", "捌");
m.put("9", "玖");
String sz = "";// 当前值
if (nr != 0) {
for (int i = 0; i < arr.length; i++) {
// 除10取余
nr = no % 10;//nr初始值为输入值,后为取余值,即当前值的阿拉伯形式。。
no = no / 10;
sz = m.get(nr.toString());// 获取值
if (nr == 0 && i != 0&&i!=4&&i!=8) {// i为零时从MAP中找不到对应的单位.
System.out.println("当前值为0此时i="+i);
result = result.insert(0, sz);// 将当前值放在字符串最前面.
} else if (nr != 0&&i!=0) {
System.out.println("当前值不为0此时i="+i);
result = result.insert(0, m.get(Integer.toString(10 + i)));// 获取单位
result = result.insert(0, sz);// 将当前值放在字符串最前面.
}else if (i==0&&nr==0){
System.out.println("当前值为个位且为0此时i="+i);
result = result.insert(0, "");// 将当前值放在字符串最前面.
}else if (i==0&&nr!=0){
System.out.println("当前值为个位且不为0此时i="+i);
result = result.insert(0, sz);// 将当前值放在字符串最前面.
}else if (nr == 0 && i != 0&&(i==4||i==8)){
System.out.println("当前值为0此时i=48+"+i);
result = result.insert(0, m.get(Integer.toString(10 + i)));// 获取单位
}
}
} else {
return result.insert(0, "零");
}
return result;
}
}
最后实思绪实在太乱了,直接用正则处理StringBuffer了 |
|