数学求解之金额转换

金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)->(一千零一拾一元整)输出。

import java.text.NumberFormat;
import java.util.HashMap;

public class SimpleMoneyFormat {

	public static final String EMPTY = "";
	public static final String ZERO = "零";
	public static final String ONE = "壹";
	public static final String TWO = "贰";
	public static final String THREE = "叁";
	public static final String FOUR = "肆";
	public static final String FIVE = "伍";
	public static final String SIX = "陆";
	public static final String SEVEN = "柒";
	public static final String EIGHT = "捌";
	public static final String NINE = "玖";
	public static final String TEN = "拾";
	public static final String HUNDRED = "佰";
	public static final String THOUSAND = "仟";
	public static final String TEN_THOUSAND = "万";
	public static final String HUNDRED_MILLION = "亿";
	public static final String YUAN = "元";
	public static final String JIAO = "角";
	public static final String FEN = "分";
	public static final String DOT = ".";
	private static SimpleMoneyFormat formatter = null;
	private HashMap chineseNumberMap = new HashMap();
	private HashMap chineseMoneyPattern = new HashMap();
	private NumberFormat numberFormat = NumberFormat.getInstance();

	private SimpleMoneyFormat() {
		numberFormat.setMaximumFractionDigits(4);
		numberFormat.setMinimumFractionDigits(2);
		numberFormat.setGroupingUsed(false);
		chineseNumberMap.put("0", ZERO);
		chineseNumberMap.put("1", ONE);
		chineseNumberMap.put("2", TWO);
		chineseNumberMap.put("3", THREE);
		chineseNumberMap.put("4", FOUR);
		chineseNumberMap.put("5", FIVE);
		chineseNumberMap.put("6", SIX);
		chineseNumberMap.put("7", SEVEN);
		chineseNumberMap.put("8", EIGHT);
		chineseNumberMap.put("9", NINE);
		chineseNumberMap.put(DOT, DOT);
		chineseMoneyPattern.put("1", TEN);
		chineseMoneyPattern.put("2", HUNDRED);
		chineseMoneyPattern.put("3", THOUSAND);
		chineseMoneyPattern.put("4", TEN_THOUSAND);
		chineseMoneyPattern.put("5", TEN);
		chineseMoneyPattern.put("6", HUNDRED);
		chineseMoneyPattern.put("7", THOUSAND);
		chineseMoneyPattern.put("8", HUNDRED_MILLION);
	}

	public synchronized static SimpleMoneyFormat getInstance() {
		if (formatter == null)
			formatter = new SimpleMoneyFormat();
		return formatter;
	}

	public String format(String moneyStr) {
		checkPrecision(moneyStr);
		String result;
		result = convertToChineseNumber(moneyStr);
		result = addUnitsToChineseMoneyString(result);
		return result;
	}

	public String format(double moneyDouble) {
		return format(numberFormat.format(moneyDouble));
	}

	public String format(int moneyInt) {
		return format(numberFormat.format(moneyInt));
	}

	public String format(long moneyLong) {
		return format(numberFormat.format(moneyLong));
	}

	public String format(Number moneyNum) {
		return format(numberFormat.format(moneyNum));
	}

	private String convertToChineseNumber(String moneyStr) {
		String result;
		StringBuffer cMoneyStringBuffer = new StringBuffer();
		for (int i = 0; i < moneyStr.length(); i++) {// 123363
			cMoneyStringBuffer.append(chineseNumberMap.get(moneyStr.substring(
					i, i + 1)));
		}
		// 拾佰仟万亿等都是汉字里面才有的单位,加上它们
		int indexOfDot = cMoneyStringBuffer.indexOf(DOT);
		int moneyPatternCursor = 1;
		for (int i = indexOfDot - 1; i > 0; i--) {
			cMoneyStringBuffer.insert(i, chineseMoneyPattern.get(EMPTY
					+ moneyPatternCursor));
			moneyPatternCursor = moneyPatternCursor == 8 ? 1
					: moneyPatternCursor + 1;
		}
		String fractionPart = cMoneyStringBuffer.substring(cMoneyStringBuffer.indexOf("."));
		cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf("."),
				cMoneyStringBuffer.length());
		while (cMoneyStringBuffer.indexOf("零拾") != -1) {// inclusive. exclusive.
			cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零拾"),
					cMoneyStringBuffer.indexOf("零拾") + 2, ZERO);
		}
		while (cMoneyStringBuffer.indexOf("零佰") != -1) {
			cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零佰"),
					cMoneyStringBuffer.indexOf("零佰") + 2, ZERO);
		}
		while (cMoneyStringBuffer.indexOf("零仟") != -1) {
			cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零仟"),
					cMoneyStringBuffer.indexOf("零仟") + 2, ZERO);
		}
		while (cMoneyStringBuffer.indexOf("零万") != -1) {
			cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零万"),
					cMoneyStringBuffer.indexOf("零万") + 2, TEN_THOUSAND);
		}
		while (cMoneyStringBuffer.indexOf("零亿") != -1) {
			cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零亿"),
					cMoneyStringBuffer.indexOf("零亿") + 2, HUNDRED_MILLION);
		}
		while (cMoneyStringBuffer.indexOf("零零") != -1) {
			cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零零"),
					cMoneyStringBuffer.indexOf("零零") + 2, ZERO);
		}
		if (cMoneyStringBuffer.lastIndexOf(ZERO) == cMoneyStringBuffer.length() - 1)
			cMoneyStringBuffer.delete(cMoneyStringBuffer.length() - 1,
					cMoneyStringBuffer.length());
		cMoneyStringBuffer.append(fractionPart);
		result = cMoneyStringBuffer.toString();
		return result;
	}

	private String addUnitsToChineseMoneyString(String moneyStr) {
		String result;
		StringBuffer cMoneyStringBuffer = new StringBuffer(moneyStr);
		int indexOfDot = cMoneyStringBuffer.indexOf(DOT);
		cMoneyStringBuffer.replace(indexOfDot, indexOfDot + 1, YUAN);
		cMoneyStringBuffer.insert(cMoneyStringBuffer.length() - 1, JIAO);
		cMoneyStringBuffer.insert(cMoneyStringBuffer.length(), FEN);
		if (cMoneyStringBuffer.indexOf("零角零分") != -1)// 没有零头,加整
			cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零角零分"),
					cMoneyStringBuffer.length(), "整");
		else if (cMoneyStringBuffer.indexOf("零分") != -1)// 没有零分,加整
			cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零分"),
					cMoneyStringBuffer.length(), "整");
		else {
			if (cMoneyStringBuffer.indexOf("零角") != -1)
				cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf("零角"),
						cMoneyStringBuffer.indexOf("零角") + 2);
			// tmpBuffer.append("整");
		}
		result = cMoneyStringBuffer.toString();
		return result;
	}

	private void checkPrecision(String moneyStr) {// 5336.53663 10-5-1
		int fractionDigits = moneyStr.length() - moneyStr.indexOf(DOT) - 1;
		if (fractionDigits > 2)
			throw new RuntimeException("金额" + moneyStr + "的小数位多于两位。"); // 精度不能比分低
	}

	public static void main(String[] args) {
		System.out.println(getInstance().format(new Double(8951.11)));
	}
}


 

public class Method2 {
	
	public static void main(String[] args) {
		System.out.println(hangeToBig(92365.23));
		
	}
	
	/** 
     * 人民币转成大写 
     *  
     * @param value 
     * @return String 
     */
    public static String hangeToBig(double value) 
    { 
        char[] hunit = { '拾', '佰', '仟' }; // 段内位置表示 
        char[] vunit = { '万', '亿' }; // 段名表示 
        char[] digit = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' }; // 数字表示 
        long midVal = (long) (value * 100); // 转化成整形 
        String valStr = String.valueOf(midVal); // 转化成字符串 
  
        String head = valStr.substring(0, valStr.length() - 2); // 取整数部分 
        String rail = valStr.substring(valStr.length() - 2); // 取小数部分 
  
        String prefix = ""; // 整数部分转化的结果 
        String suffix = ""; // 小数部分转化的结果 
        // 处理小数点后面的数 
        if (rail.equals("00")) 
        { // 如果小数部分为0 
            suffix = "整"; 
        } 
        else
        { 
            suffix = digit[rail.charAt(0) - '0'] + "角" + digit[rail.charAt(1) - '0'] + "分"; // 否则把角分转化出来 
        } 
        // 处理小数点前面的数 
        char[] chDig = head.toCharArray(); // 把整数部分转化成字符数组 
        char zero = '0'; // 标志'0'表示出现过0 
        byte zeroSerNum = 0; // 连续出现0的次数 
        for (int i = 0; i < chDig.length; i++) 
        { // 循环处理每个数字 
            int idx = (chDig.length - i - 1) % 4; // 取段内位置 
            int vidx = (chDig.length - i - 1) / 4; // 取段位置 
            if (chDig[i] == '0') 
            { // 如果当前字符是0 
                zeroSerNum++; // 连续0次数递增 
                if (zero == '0') 
                { // 标志 
                    zero = digit[0]; 
                } 
                else if (idx == 0 && vidx > 0 && zeroSerNum < 4) 
                { 
                    prefix += vunit[vidx - 1]; 
                    zero = '0'; 
                } 
                continue; 
            } 
            zeroSerNum = 0; // 连续0次数清零 
            if (zero != '0') 
            { // 如果标志不为0,则加上,例如万,亿什么的 
                prefix += zero; 
                zero = '0'; 
            } 
            prefix += digit[chDig[i] - '0']; // 转化该数字表示 
            if (idx > 0) 
                prefix += hunit[idx - 1]; 
            if (idx == 0 && vidx > 0) 
            { 
                prefix += vunit[vidx - 1]; // 段结束位置应该加上段名如万,亿 
            } 
        } 
  
        if (prefix.length() > 0) 
            prefix += '圆'; // 如果整数部分存在,则有圆的字样 
        return prefix + suffix; // 返回正确表示 
    }

}


 

你可能感兴趣的:(数学求解之金额转换)