package cn.hmsoft.helper;

import java.math.BigDecimal;

public class NumericHelper {
	public static final String CURRENCY_FEN_REGEX = "\\-?[0-9]+";

	public static String changeF2Y(Long amount) throws Exception {
		if (!amount.toString().matches("\\-?[0-9]+")) {
			throw new Exception("金额格式有误");
		}
		int flag = 0;
		String amString = amount.toString();
		if (amString.charAt(0) == '-') {
			flag = 1;
			amString = amString.substring(1);
		}
		StringBuffer result = new StringBuffer();
		if (amString.length() == 1) {
			result.append("0.0").append(amString);
		} else if (amString.length() == 2) {
			result.append("0.").append(amString);
		} else {
			String intString = amString.substring(0, amString.length() - 2);
			for (int i = 1; i <= intString.length(); i++) {
				if (((i - 1) % 3 == 0) && (i != 1)) {
					result.append(",");
				}
				result.append(intString.substring(intString.length() - i,
						intString.length() - i + 1));
			}
			result.reverse().append(".")
					.append(amString.substring(amString.length() - 2));
		}
		if (flag == 1) {
			return "-" + result.toString();
		}
		return result.toString();
	}

	public static String changeF2Y(String amount) throws Exception {
		if (!amount.matches("\\-?[0-9]+")) {
			throw new Exception("金额格式有误");
		}
		return BigDecimal.valueOf(Long.valueOf(amount).longValue())
				.divide(new BigDecimal(100)).toString();
	}

	public static String changeY2F(Long amount) {
		return BigDecimal.valueOf(amount.longValue())
				.multiply(new BigDecimal(100)).toString();
	}

	public static String changeY2F(String amount) {
		String currency = amount.replaceAll("\\$|\\￥|\\,", "");
		int index = currency.indexOf(".");
		int length = currency.length();
		Long amLong = Long.valueOf(0L);
		if (index == -1) {
			amLong = Long.valueOf(currency + "00");
		} else if (length - index >= 3) {
			amLong = Long.valueOf(currency.substring(0, index + 3).replace(".",
					""));
		} else if (length - index == 2) {
			amLong = Long.valueOf(currency.substring(0, index + 2).replace(".",
					"") + 0);
		} else {
			amLong = Long.valueOf(currency.substring(0, index + 1).replace(".",
					"")
					+ "00");
		}
		return amLong.toString();
	}

	public static String transition(String si) {
		String[] aa = { "", "十", "百", "千", "万", "十万", "百万", "千万", "亿", "十亿" };
		String[] bb = { "一", "二", "三", "四", "五", "六", "七", "八", "九" };
		char[] ch = si.toCharArray();
		int maxindex = ch.length;
		String str = "";
		if (maxindex == 2) {
			int i = maxindex - 1;
			for (int j = 0; i >= 0; j++) {
				if (ch[j] != '0') {
					if ((j == 0) && (ch[j] == '1')) {
						str = str + aa[i];
					} else {
						str = str + bb[(ch[j] - '1')] + aa[i];
					}
				}
				i--;
			}
		} else {
			int i = maxindex - 1;
			for (int j = 0; i >= 0; j++) {
				if (ch[j] != '0') {
					str = str + bb[(ch[j] - '1')] + aa[i];
				}
				i--;
			}
		}
		return str;
	}

	public static String digitUppercase(double n) {
		String[] fraction = { "角", "分" };
		String[] digit = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
		String[][] unit = { { "元", "万", "亿" }, { "", "拾", "佰", "仟" } };

		String head = n < 0.0D ? "负" : "";
		n = Math.abs(n);

		String s = "";
		for (int i = 0; i < fraction.length; i++) {
			s = s
					+ new StringBuilder(String.valueOf(digit[((int) (Math
							.floor(n * 10.0D * Math.pow(10.0D, i)) % 10.0D))]))
							.append(fraction[i]).toString()
							.replaceAll("(零.)+", "");
		}
		if (s.length() < 1) {
			s = "整";
		}
		int integerPart = (int) Math.floor(n);
		for (int i = 0; (i < unit[0].length) && (integerPart > 0); i++) {
			String p = "";
			for (int j = 0; (j < unit[1].length) && (n > 0.0D); j++) {
				p = digit[(integerPart % 10)] + unit[1][j] + p;
				integerPart /= 10;
			}
			s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i]
					+ s;
		}
		return head
				+ s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "")
						.replaceAll("(零.)+", "零").replaceAll("^整$", "零元整");
	}

	public static void main(String[] args) throws Exception {
		String str = digitUppercase(300.0D);
		System.out.println(str);
	}
}
