1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| public String digitUppercase(String num) throws Exception{ String fraction[] = {"角", "分"}; String digit[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; String unit1[] = {"", "拾", "佰", "仟"}; String unit2[] = {"元", "万", "亿","万亿"}; BigDecimal bigDecimal = new BigDecimal(num); bigDecimal=bigDecimal.multiply(new BigDecimal(100)); String strVal = String.valueOf(bigDecimal.toBigInteger()); if("0".equals(strVal)){ strVal = "000"; } String head = strVal.substring(0,strVal.length()-2); String end = strVal.substring(strVal.length()-2); String endMoney=""; String headMoney = ""; if("00".equals(end)){ endMoney = "整"; }else{ if(!end.substring(0,1).equals("0")){ endMoney+=digit[Integer.valueOf(end.substring(0,1))]+"角"; }else if(end.substring(0,1).equals("0") && !end.substring(1,2).equals("0")){ endMoney+= "零"; } if(!end.substring(1,2).equals("0")){ endMoney+=digit[Integer.valueOf(end.substring(1,2))]+"分"; } } char[] chars = head.toCharArray(); Map<String,Boolean> map = new HashMap<String,Boolean>(); boolean zeroKeepFlag = false; int vidxtemp = 0; for(int i=0;i<chars.length;i++){ int idx = (chars.length-1-i)%4; int vidx = (chars.length-1-i)/4; String s = digit[Integer.valueOf(String.valueOf(chars[i]))]; if(!"零".equals(s)){ headMoney += s +unit1[idx]+unit2[vidx]; zeroKeepFlag = false; }else if(i==chars.length-1 || map.get("zero"+vidx)!=null){ headMoney += "" ; }else{ headMoney += s; zeroKeepFlag = true; map.put("zero"+vidx,true); } if(vidxtemp!=vidx || i==chars.length-1){ headMoney = headMoney.replaceAll(unit2[vidx],""); headMoney+=unit2[vidx]; } if(zeroKeepFlag && (chars.length-1-i)%4==0){ headMoney = headMoney.replaceAll("零",""); } } return "元整".equals(headMoney+endMoney)?"零元整":headMoney+endMoney; }
|