将数字转换为汉字书写模式
面试i遇到这样一个题:
将23009912340转换为"二百三十亿零九百九十一万二千三百四十",当时没想到什么好的方法,个人感觉"零"最难处理,代码如下:
import java.util.HashMap; import java.util.Map; import java.util.Set; public class ToChina { public static final String[] st = new String[] { "千", "百", "十", "" }; public String convert(int i) { String china = null; switch (i) { case 0: china = "零"; break; case 1: china = "一"; break; case 2: china = "二"; break; case 3: china = "三"; break; case 4: china = "四"; break; case 5: china = "五"; break; case 6: china = "六"; break; case 7: china = "七"; break; case 8: china = "八"; break; case 9: china = "九"; break; } return china; } public Map<String, Boolean> buildComplete(String s) { char[] chars = s.toCharArray(); String[] strs = new String[] { null, null, null, null }; int nullnum = 4 - s.length(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < chars.length; i++) { strs[i + nullnum] = String.valueOf(chars[i]); } Boolean flag = false; Boolean f = false; for (int i = strs.length - 1; i >= 0; i--) { if (f == false && strs[i] != null && strs[i].equals("零")) { strs[i] = null; flag = true; } if (strs[i] != null && !strs[i].equals("零")) { f = true; break; } } Boolean b = false; for (int i = 0; i < 4; i++) { if (strs[i] != null) { if (!strs[i].equals("零")) { sb.append(strs[i] + st[i]); } if (strs[i].equals("零") && b == false) { sb.append(strs[i]); b = true; } } } Map<String, Boolean> map = new HashMap<String, Boolean>(); map.put(sb.toString(), flag); return map; } public String[] split(String str) { int length = str.length(); int num = length / 4; int mo = length % 4; String[] ss = mo > 0 ? new String[num + 1] : new String[num]; if (length <= 4) { ss = new String[] { str }; } else { for (int i = 0; i < ss.length; i++) { if (i == 0) { ss[i] = str.substring(0, mo); } else { ss[i] = str.substring(mo + (i - 1) * 4, mo + (i) * 4); } } } return ss; } public String danwei(Map<String, Boolean> map, int i, Boolean flag) { String v = null; if (i == 3) { v = "兆"; } if (i == 2) { v = "亿"; } if (i == 1) { v = "万"; } if (i == 0) { v = ""; } StringBuffer sb = new StringBuffer(); Set<String> keyset = map.keySet(); for (String key : keyset) { if (!key.equals("")) { sb.append(key); sb.append(v); if (map.get(key) == true && i != 0) { sb.append("零"); } if (flag == true) { sb.deleteCharAt(0); } } } return sb.toString(); } public String result(String str) { StringBuffer sb = new StringBuffer(); String[] strs = this.split(this.convertAll(str)); int k = 1; for (int i = 0; i < strs.length; i++) { int j = strs.length - k; k += 1; boolean flag = false; if (i - 1 >= 0) { Map map = this.buildComplete(strs[i - 1]); Set<String> keyset = map.keySet(); for (String key : keyset) { flag = (Boolean) map.get(key); break; } } String s = this.danwei(this.buildComplete(strs[i]), j, flag); sb.append(s); } return sb.toString(); } public String convertAll(String str) { char[] chars = str.toCharArray(); String[] cs = new String[chars.length]; for (int i = 0; i < chars.length; i++) { cs[i] = this.convert(Integer.valueOf(String.valueOf(chars[i]))); } StringBuffer sb = new StringBuffer(); for (int i = 0; i < cs.length; i++) { sb.append(cs[i]); } return sb.toString(); } public static void main(String[] args) { ToChina t = new ToChina(); System.out.print(t.result("23009912340")); } }