金额转换(数字转中文)

public static void main(String[] args) { double number = 23003600.00; DecimalFormat format = new DecimalFormat("#.00"); String num= format.format(number); System.out.println("转换前数字值:"+num); String result = numCoverChinese(num); System.out.println("转换后结果值:"+result); }/** * 金额数字转中文 * @param number * @return */ public static String numCoverChinese(String number){ String[] split = number.split("\\."); //根据.截取整数部分和Decimal点后的部分 String integers = split[0]; String scores = split[1]; StringBuffer stringBuffer = new StringBuffer(); //4位以内整数部分进行转换 if(integers.length()>0 && integers.length() < 5) { getResultByNum(integers, stringBuffer, 1); } //8位以内整数部分进行转换 if(integers.length()>4 && integers.length() < 9){ String substring = integers.substring(0, 4); getResultByNum(substring, stringBuffer, 2); String substring1 = integers.substring(4, integers.length()); getResultByNum(substring1, stringBuffer, 1); } //12位以内整数部分进行转换 if(integers.length()>8 && integers.length() < 13){ String substring = integers.substring(0, 4); getResultByNum(substring, stringBuffer, 3); String substring1 = integers.substring(4, 8); getResultByNum(substring1, stringBuffer, 2); String substring2 = integers.substring(8, integers.length()); getResultByNum(substring2, stringBuffer, 1); } //小数点后部分进行转换 if(scores.length()>0 && !scores.equals("00")){ //定义截取的字符串位数 int sem = 0; for (int i=0; i0){ String substring1 = stringBuffer.substring(stringBuffer.length() - 1, stringBuffer.length()); if(substring1.equals("零") && chinese.equals("零")){}else { stringBuffer.append(chinese); } }else { stringBuffer.append(chinese); } if(!chinese.equals("零")){ String unit = getUnit(sem + 1); stringBuffer.append(unit); } sem++; } //判断最后一位是否为零 为零则去掉 String substring1 = stringBuffer.substring(stringBuffer.length() - 1, stringBuffer.length()); if(substring1.equals("零")){ stringBuffer.deleteCharAt(stringBuffer.length() - 1); } if(digit.equals(1)){ stringBuffer.append("元"); } if(digit.equals(2)){ stringBuffer.append("万"); } if(digit.equals(3)){ stringBuffer.append("亿"); } return stringBuffer; }//根据数字获取对应中文 public static String getChinese(String digital){ String result = ""; switch (digital){ case "0":{ result = "零"; break; } case "1":{ result = "壹"; break; } case "2":{ result = "贰"; break; } case "3":{ result = "叁"; break; } case "4":{ result = "肆"; break; } case "5":{ result = "伍"; break; } case "6":{ result = "陆"; break; } case "7":{ result = "柒"; break; } case "8":{ result = "捌"; break; } case "9":{ result = "玖"; break; } default:{ break; } } return result; }/** * 根据位数取小数点后单位 * @param digits * @return */ public static String getDecimalUnit(Integer digits){ String result = ""; switch (digits){ case 1:{ result = "角"; break; } case 2:{ result = "分"; break; } default:{ break; } } return result; }/** * 根据位数取单位 * @param digits * @return */ public static String getUnit(Integer digits){ String result = ""; switch (digits){ case 1:{ result = "仟"; break; } case 2:{ result = "佰"; break; } case 3:{ result = "十"; break; } default:{ break; } } return result; }


【金额转换(数字转中文)】运行结果:
金额转换(数字转中文)
文章图片

目前支持以12位数内计算,如需增加位数,则添加位数判断解决

    推荐阅读