package numToCh; /** */ /** * @author 毛朝志 华北电力大学 计算0401(请保留!) * 欢迎汇报错误和提出改进意见 QQ:406804063 Email:maochaozhi101@163.com * */ public class numToChRMB ... { private static final String chs[] = ...{ "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾" }; /** *//** * * @param money 金额数字串 * @return 金额中文大写 * 将输入数字串money分割成4位数,将四位数转换成金额大写后再加上单位("","万","亿") */ public static String numToRMB(String money) ...{ StringBuilder chRMB = new StringBuilder(); money=money.replaceAll("[,,]", "");//去除分隔符逗号 try ...{ double t = Double.parseDouble(money); if (t < 0) ...{ System.out.println("金额不能是负的!"); throw new IllegalArgumentException("金额不能是负的!"); //return null; } } catch (NumberFormatException e) ...{ System.out.println("输入数据有误,请检查!"); throw new IllegalArgumentException("输入数据有误,请检查!"); //return null; } int dotPos = money.indexOf('.'); String strz;//截取整数部分 if(dotPos==-1)...{ strz=money; } else...{ strz=money.substring(0, dotPos);//整数部分 } StringBuilder sb=new StringBuilder(strz); while(sb.length()>=1&&sb.charAt(0)=='0')...{// 去掉数前面多余的零 sb.deleteCharAt(0); } if(sb.length()==0)...{//输入为‘0...0’的情况 return "零"; } strz=sb.toString(); String cur=""; boolean zero=false;//记录是否应该加‘零’如 1,0000,0000,0001,读作 壹万亿零壹圆整 String temp; String digits;//每次截取的最后四位数 while(strz.length()>0)...{ if(strz.length()>=4)...{ digits=strz.substring(strz.length()-4); strz=strz.substring(0, strz.length()-4); } else...{ digits=strz; strz=""; } temp=fourMaxChange(digits); if(temp.equals("零"))...{ if(cur.equals("亿"))...{ chRMB.insert(0, "亿"); } if (zero == true) ...{ if (!chRMB.substring(0, 1).equals("零")) chRMB.insert(0, "零"); zero = false; } } else ...{ temp += cur; chRMB.insert(0, temp); if (digits.charAt(0) == '0') if (chRMB.length() >=1 && !chRMB.substring(0, 1).equals("零")) chRMB.insert(0, "零"); zero = true; } if(cur.equals(""))...{//改变单位 cur="万"; } else if(cur.equals("亿"))...{ cur="万"; } else cur="亿"; } String strx; if(dotPos!=-1)...{//有小数 strx = money.substring(dotPos+1); if (strx.length() > 2) ...{ System.out.println("金额只允许两位小数!"); throw new IllegalArgumentException("金额只允许两位小数!"); //return null; } int xiaoshu = Integer.parseInt(strx);//小数部分 if(xiaoshu==0||strx.length()==0)...{ chRMB.append("圆整");//整 } else chRMB.append("圆"); if(strx.length()==1)...{ chRMB.append(chs[xiaoshu]).append("分"); } else...{ if(xiaoshu/10!=0)...{ chRMB.append(chs[xiaoshu/10]).append("角"); } else...{ chRMB.append("零"); } if(xiaoshu%10!=0)...{ chRMB.append(chs[xiaoshu%10]).append("分"); } } } else...{ chRMB.append("圆整"); } return chRMB.toString(); } /** *//** * * @param number 输入数字串 * @return 转换后的大写金额 * @throws NumberFormatException 不合法的数字 * 将最多四位整数转换成大写金额 */ public static String fourMaxChange(String number) throws NumberFormatException ...{// 将最多四位整数转换成大写金额 StringBuilder rs = new StringBuilder(); int money; money = Integer.parseInt(number); boolean zero = false; if (money / 1000 != 0) ...{ zero = true; rs.append(chs[money / 1000]).append("仟"); money %= 1000; } if (money / 100 != 0) ...{ zero = true; rs.append(chs[money / 100]).append("佰"); money %= 100; } else if (zero == true) ...{ rs.append(chs[0]);// 零 money %= 100; } if (money / 10 != 0) ...{ zero = true; rs.append(chs[money / 10]).append("拾"); money %= 10; } else if (zero == true && !rs.substring(rs.length() - 1).equals("零")) ...{ rs.append(chs[0]);// 零 money %= 10; } if (money != 0) ...{ zero = true; rs.append(chs[money]); } else if (zero == true && rs.substring(rs.length() - 1).equals("零")) ...{ rs.delete(rs.length() - 1, rs.length()); } if (zero == true) return rs.toString(); else return chs[0];//零 } public static void main(String[] args) ...{ System.out.println(numToRMB("12,0000,0000,0403,0012,0000.02")); }}