金额转换(数字转中文)

public static void mainString[] args) {double number = 23003600.00;DecimalFormat format = new DecimalFormat"#.00");String num= format.formatnumber);System.out.println"转换前数字值:"+num);String result = numCoverChinesenum);System.out.println"转换后结果值:"+result);}/*** 金额数字转中文* @param number* @return*/public static String numCoverChineseString number){String[] split = number.split"\\.");//根据.截取整数部分和Decimal点后的部分String integers = split[0];String scores = split[1];StringBuffer stringBuffer = new StringBuffer);//4位以内整数部分进行转换ifintegers.length)>0 && integers.length) < 5) {getResultByNumintegers, stringBuffer, 1);}//8位以内整数部分进行转换ifintegers.length)>4 && integers.length) < 9){String substring = integers.substring0, 4);getResultByNumsubstring, stringBuffer, 2);String substring1 = integers.substring4, integers.length));getResultByNumsubstring1, stringBuffer, 1);}//12位以内整数部分进行转换ifintegers.length)>8 && integers.length) < 13){String substring = integers.substring0, 4);getResultByNumsubstring, stringBuffer, 3);String substring1 = integers.substring4, 8);getResultByNumsubstring1, stringBuffer, 2);String substring2 = integers.substring8, integers.length));getResultByNumsubstring2, stringBuffer, 1);}//小数点后部分进行转换ifscores.length)>0 && !scores.equals"00")){//定义截取的字符串位数int sem = 0;for int i=0;i<scores.length);i++){String substring = scores.substringsem, sem + 1);String chinese = getChinesesubstring);ifsem == 1 && chinese.equals"零")){break;}stringBuffer.appendchinese);if!chinese.equals"零")){String decimalUnit = getDecimalUnitsem + 1);stringBuffer.appenddecimalUnit);}sem++;}}else {stringBuffer.append"整");}return stringBuffer.toString);}/*** 通过传入的位数(1代表4位以内,2代表8位以内,3代表9位以上)获取值* @param integers* @param stringBuffer* @param digit* @return*/public static StringBuffer getResultByNumString integers,StringBuffer stringBuffer,Integer digit){//定义截取的字符串位数int sem = 0;for int i=0;i<integers.length);i++){String substring = integers.substringsem, sem + 1);String chinese = getChinesesubstring);ifstringBuffer.length)>0){String substring1 = stringBuffer.substringstringBuffer.length) - 1, stringBuffer.length));ifsubstring1.equals"零") && chinese.equals"零")){}else {stringBuffer.appendchinese);}}else {stringBuffer.appendchinese);}if!chinese.equals"零")){String unit = getUnitsem + 1);stringBuffer.appendunit);}sem++;}//判断最后一位是否为零 为零则去掉String substring1 = stringBuffer.substringstringBuffer.length) - 1, stringBuffer.length));ifsubstring1.equals"零")){stringBuffer.deleteCharAtstringBuffer.length) - 1);}ifdigit.equals1)){stringBuffer.append"元");}ifdigit.equals2)){stringBuffer.append"万");}ifdigit.equals3)){stringBuffer.append"亿");}return stringBuffer;}//根据数字获取对应中文public static String getChineseString 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 getDecimalUnitInteger digits){String result = "";switch digits){case 1:{result = "角";break;}case 2:{result = "分";break;}default:{break;}}return result;}/*** 根据位数取单位* @param digits* @return*/public static String getUnitInteger digits){String result = "";switch digits){case 1:{result = "仟";break;}case 2:{result = "佰";break;}case 3:{result = "十";break;}default:{break;}}return result;}

 

运行结果:

 

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

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注