金額の変換——アラビア数字の金額は中国の伝統的な形式に変換します

6995 ワード

package com.test.coding.theme;

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

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 NumberFormat numberFormat = NumberFormat.getInstance();

    private Map<String, String> chineseMap = new HashMap<String, String>();

    private Map<Integer, String> unitMap = new HashMap<Integer, String>();

    public SimpleMoneyFormat() {
        numberFormat.setGroupingUsed(false); //   100,100,1

        chineseMap.put("0", ZERO);
        chineseMap.put("1", ONE);
        chineseMap.put("2", TWO);
        chineseMap.put("3", THREE);
        chineseMap.put("4", FOUR);
        chineseMap.put("5", FIVE);
        chineseMap.put("6", SIX);
        chineseMap.put("7", SEVEN);
        chineseMap.put("8", EIGHT);
        chineseMap.put("9", NINE);
        chineseMap.put(DOT, DOT);

        unitMap.put(1, TEN);
        unitMap.put(2, HUNDRED);
        unitMap.put(3, THOUSAND);
        unitMap.put(4, TEN_THOUSAND);
        unitMap.put(5, TEN);
        unitMap.put(6, HUNDRED);
        unitMap.put(7, THOUSAND);
        unitMap.put(8, HUNDRED_MILLION);
    }

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

    public String convertToChinese(String moneyStr) {
        StringBuffer sb = new StringBuffer(EMPTY);
        for (int i = 0; i < moneyStr.length(); i++) {
            sb.append(chineseMap.get(moneyStr.substring(i, i + 1)));
        }
        int indexOfDot = sb.indexOf(DOT);
        int length = indexOfDot != -1 ? indexOfDot - 1 : sb.length() - 1;
        int cursor = 1;
        for (int i = length; i > 0; i--) {
            sb.insert(i, unitMap.get(cursor));
            if (cursor == 8) {
                cursor = 1;
            } else {
                cursor = cursor + 1;
            }
        }
        String dotPart = EMPTY;
        if (sb.indexOf(DOT) != -1) {
            dotPart = sb.substring(sb.indexOf(DOT));
            sb.delete(sb.indexOf(DOT), sb.length());
        }
        while (sb.indexOf(" ") != -1) {
            sb.replace(sb.indexOf(" "), sb.indexOf(" ") + 2, ZERO);
        }
        while (sb.indexOf(" ") != -1) {
            sb.replace(sb.indexOf(" "), sb.indexOf(" ") + 2, ZERO);
        }
        while (sb.indexOf(" ") != -1) {
            sb.replace(sb.indexOf(" "), sb.indexOf(" ") + 2, ZERO);
        }
        while (sb.indexOf(" ") != -1) {
            sb.replace(sb.indexOf(" "), sb.indexOf(" ") + 2, TEN_THOUSAND);
        }
        while (sb.indexOf(" ") != -1) {
            sb.replace(sb.indexOf(" "), sb.indexOf(" ") + 2, HUNDRED_MILLION);
        }
        while (sb.indexOf(" ") != -1) {
            sb.replace(sb.indexOf(" "), sb.indexOf(" ") + 2, ZERO);
        }
        if (sb.lastIndexOf(ZERO) == sb.length() - 1) {
            sb.delete(sb.length() - 1, sb.length());
        }
        
        if (EMPTY.equals(dotPart) || DOT.equals(dotPart)) {
            dotPart = DOT + ZERO + ZERO;
        } else if (dotPart.length() == 2) {
            dotPart = dotPart + ZERO;
        } else if (dotPart.length() > 3) {
            dotPart = dotPart.substring(0, 3); //  
        }
        sb.append(dotPart);
        return sb.toString();
    }

    public String addUnit(String str) {
        StringBuffer sb = new StringBuffer(str);
        int indexOfDot = sb.indexOf(DOT);
        if (indexOfDot != -1) {
            sb.replace(indexOfDot, indexOfDot + 1, YUAN);
            sb.insert(sb.length() - 1, JIAO);
            sb.insert(sb.length(), FEN);
        } else {
            sb.append(YUAN);
        }
        while (sb.indexOf(" ") != -1) {
            sb.replace(sb.indexOf(" "), sb.indexOf(" ") + 2, EMPTY);
        }
        while (sb.indexOf(" ") != -1) {
            sb.replace(sb.indexOf(" "), sb.indexOf(" ") + 2, EMPTY);
        }
        return sb.toString();
    }

    public String format(String money) {
        String result = convertToChinese(money);
        result = addUnit(result);
        return result;
    }
    
    public String format(double d) {
        return format(numberFormat.format(d));
    }
    
    public String format(int i) {
        return format(numberFormat.format(i));
    }
    
    public String format(long l) {
        return format(numberFormat.format(l));
    }
    
    public String format(Number number) {
        return format(numberFormat.format(number));
    }

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

 
出力結果:
贰万叁仟亿零肆万肆仟叁佰贰拾肆元贰分