10、 金额转换,阿拉伯数字转换成中国传统形式。
例如:101000001010 转换为 壹仟零壹拾亿零壹仟零壹拾圆整
下面是代码,离要求还差远了.
package com.jwd.black.exam;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
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();
numToWord(s);
}
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 = (long) 0;
String key = "";
String value = "";
for (int j = arr.length; j > 0; j--) {
key = Integer.toString(10 + j);
if (j != 0 && 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", "玖");
for (int i = 0; i < arr.length; i++) {
// 除10取余
nr = no % 10;
no = no / 10;
if (i != 0)
result = result.insert(0, m.get(Integer.toString(10 + i)));
if (nr != 0)//如为0转化为字符串时会变为NULL
result = result.insert(0, m.get(nr.toString()));
else
result = result.insert(0, "零");
System.out.println(result);
}
return result;
}
}
输出结果:壹仟零佰壹拾零亿零仟零佰零拾零万壹仟零佰壹拾零圆整 |
|