java Stringutil 字符串(内含金额转换为中文)

阅读更多
今天主要写了这个金额转换的方法,这个金额转换最大支持九仟万亿兆以内的金额转换,最小支持0.0009厘的金额转换,应该可以对现在的金额进去转换运算了
因为金额支持过大,所以我测试金额肯定不全,如有错误,还请大家指正,
我在以后还会慢慢完善这个字符串工具类的~~
package com.jynine.main;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * 金额转换 支持99999999999999999999999999999999.9999以内的金额换算,
 * 应该够目前的金额使用了。
 * @author jynine
 *
 */
public class MoneyConvert {
	private static String[] NUMBER= {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
	private static String[] UN = {"","万","亿","兆"};
	private static String[] CH_1 ={"仟","","拾","佰"};
	private static String[] CH_2 = {"角","分","毫","厘"};
	private static int ZERO = 0;
	private static Pattern FLOATPATTERN = Pattern.compile("^(-?\\d+)(\\.\\d{1,4})?$");
	/**
	 * 金额转换(只精确到厘)
	 * @param num
	 * @return
	 * @throws Exception 
	 */
	public static String convert(String money) throws Exception{
		if(isEmpty(money == null ? null : money.replaceAll(" ", ""))){
			throw new Exception("金额为空!");
		}else{
			money = money.replaceAll(" ", "");
			if(!isFloat(money)){
				throw new Exception("金额格式不正确!");
			}else{
				StringBuffer sb = new StringBuffer();
				String pointMoney = "";
				sb.append("¥");
				if(money.contains(".")){
					pointMoney = money.substring(money.indexOf(".")+1, money.length());
					money = money.substring(0, money.indexOf("."));
				}
				if(money.length() > 32){
					throw new Exception("目前只支持32位及以下的金额!");
				}else{
					int zeroCount = 0;
					recursionMoney(sb, money, zeroCount);
					boolean tag = true;
					if(money.length() == 1 && money.equals("0")){
						zeroCount = 0;
						tag = false;
					}else{					
						sb.append("元");
					}
					calPoint(sb,pointMoney,zeroCount,tag);
					sb.append("整");
					return sb.toString();
				}
			}
		}
	}
	/**
	 * 递归换算money
	 * @param sb
	 * @param money
	 * @param zeroCount
	 */
	private static void recursionMoney(StringBuffer sb,String money,int zeroCount){
		int len = money.length();
		int end = 8;
		if(len%8 != 0){
			end = len%8;
		}
		calMoney(sb, money.substring(0, end), zeroCount);
		if(len > 8){			
			sb.append(UN[(((len-1)/8))%2==0?3:2]);
		}
		money = money.substring(end, money.length());
		if(isNotEmpty(money) && money.length()>0){			
			recursionMoney(sb, money, zeroCount);
		}
	}
	/**
	 * 分开计算money
	 * @param sb
	 * @param money
	 * @return
	 */
	private static StringBuffer calMoney(StringBuffer sb,String money,int zeroCount){
		int len = money.length();
		String s = "";
		int n = 0;
		for (int i = 0; i < len; i++) {
			s = money.substring(i, i+1);
			n = Integer.parseInt(s);
			if((len-i)%4 == 1){
				if(n != ZERO){
					if(zeroCount > 0){
						sb.append(NUMBER[0]);
					}
					sb.append(NUMBER[n] + CH_1[(len-i)%4]);
					zeroCount = 0;
				}else{
					zeroCount ++;
				}
				if(zeroCount < 4){					
					sb.append(UN[(len-i)/4]);
				}
			}else{
				if(n != ZERO){
					if(zeroCount > 0){
						sb.append(NUMBER[0]);
					}
					sb.append(NUMBER[n]+CH_1[(len-i)%4]);
					zeroCount = 0;
				}else{
					zeroCount ++;
				}
				
			}
		}
		return sb;
	}
	/**
	 * 小数点金额转换
	 * @param sb
	 * @param pointMoney
	 * @param zeroCount
	 * @param tag
	 */
	private static void calPoint(StringBuffer sb,String pointMoney,int zeroCount,boolean tag){
		String s = "";
		int n = 0;
		if(isNotEmpty(pointMoney)){
			for (int i = 0; i < pointMoney.length(); i++) {
				if(i>3){
					break;
				}
				s = pointMoney.substring(i, i+1);
				n = Integer.parseInt(s);
				if(n == ZERO){
					if(tag){						
						zeroCount ++;
					}
				}else{
					if(zeroCount > 0 && i != 0){
						sb.append(NUMBER[0]);
					}
					tag = true;
					sb.append(NUMBER[n]+CH_2[i]);
					zeroCount = 0;
				}
			}
		}
	}
	/**
	 * 判断字符是否是空
	 * @param s
	 * @return
	 */
	public static boolean isEmpty(String str){
		if(str != null && str!= ""){
			return false;
		}else{
			return true;
		}
	}
	/**
	 * 判断字符是否为非空
	 * @param str
	 * @return
	 */
	public static boolean isNotEmpty(String str){
		if(str != null && str!= ""){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 验证是否是float类型
	 * @param input
	 * @return
	 */
	public static boolean isFloat(String input){
		Matcher matcher = FLOATPATTERN.matcher(input);
		return matcher.matches();
	}
	public static void main(String[] args) throws Exception {
		System.out.println(convert("99999999999999999999999999999999.9999"));
		System.out.println(convert("10 0010.29"));
	}
}

你可能感兴趣的:(java,金额转换,字符串工具,兆,亿)