package test; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; public class Test { //将正整数的阿拉伯钱数转换为中文形式,如1011→一千零一十一,输出。 private static String[] chineseMapStr = {"零","一","二","三","四","五","六","七","八","九"}; private static String[] specailStr0 = {"千","百","十"}; private static String[] specailStr1 = {"亿","万"}; public static void main(String[] args) { System.out.println(convert("1090021")); } private static String convert(String numberStr){ StringBuffer buffer=new StringBuffer(); List<String> list=new ArrayList<String>(); for(int i=0,j=numberStr.length();i<numberStr.length();i++,j--){ int num=Integer.parseInt(numberStr.substring(i,i+1)); String chinessNum=chineseMapStr[num]; //九 String unit=getUnit(j); //千 if(chinessNum.equals("零")){ if(!list.isEmpty()){ String chinessNumInList=list.get(list.size()-1); if(chinessNumInList.equals("零")){ list.remove(list.size()-1); } } } list.add(chinessNum); if(!StringUtils.isEmpty(unit)){ if(chinessNum.equals("零")){ if(ArrayUtils.contains(specailStr0, unit)){ }else if(ArrayUtils.contains(specailStr1, unit)){ list.remove(list.size()-1); list.add(unit); } }else{ list.add(unit); } } } for(String num:list){ buffer.append(num); } return buffer.toString(); } /** * 位数表示,如亿、万等 * @param digit * @return */ private static String getUnit(int digit) { String unit = ""; switch(digit) { case 12: case 8: case 4: unit = "千"; break; case 11: case 7: case 3: unit = "百"; break; case 10: case 6: case 2: unit = "十"; break; case 9: unit = "亿"; break; case 5: unit = "万"; break; } return unit; } }
法2:
public class Test3 { public static String translateMoney(int money) { String total = "";//返回的字符串 String[] Money = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九"}; String[] Util = { " ","十", "百", "千", "万", "十", "百", "千", "亿"}; String strMoney = String.valueOf(money);//参数转字符串 String[] inMoney =strMoney.trim().split("");//参数转存到数组里 //此处数组为[, 9, 0, 0, 8, 7, 9] for(int i=0;i<strMoney.length();i++){ String mon = Money[Integer.parseInt(inMoney[1+i])]; String util = Util[strMoney.length()-i-1]; total += (mon + util); } return total; } public static void main(String[] args) { System.out.println(translateMoney(900879)); //九十零万零千八百七十九 有问题还需仔细处理 } }