JAvaのデータ金額の金額を大文字に変換するツールクラス

3799 ワード

1.背景
1.1.プロジェクトの必要性のため、アラビアの金額のお金の数を大文字のツールクラスに変換する必要があります.次は直接コードをつけます.
2.Util
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class ChineseYuanUtil {
 
    private static final Pattern AMOUNT_PATTERN = Pattern.compile("^(0|[1-9]\\d{0,11})\\.(\\d\\d)$"); //           
    private static final char[] RMB_NUMS = "          ".toCharArray();
    private static final String[] UNITS = { " ", " ", " ", " " };
    private static final String[] U1 = { "", " ", " ", " " };
    private static final String[] U2 = { "", " ", " " };
 
    /**
     *    (          12  ,     2  )         .
     * 
     * @param amount     
     * @return     
     * @throws IllegalArgumentException
     */
    public static String convert(String amount) throws IllegalArgumentException {
        //      
        amount = amount.replace(",", "");
 
        //        
        if (amount.equals("0.00")) {
            throw new IllegalArgumentException("      .");
        }
        Matcher matcher = AMOUNT_PATTERN.matcher(amount);
        if (!matcher.find()) {
            throw new IllegalArgumentException("      .");
        }
 
        String integer = matcher.group(1); //     
        String fraction = matcher.group(2); //     
 
        String result = "";
        if (!integer.equals("0")) {
            result += integer2rmb(integer) + UNITS[0]; //     
        }
        if (fraction.equals("00")) {
            result += UNITS[3]; //   [ ]
        } else if (fraction.startsWith("0") && integer.equals("0")) {
            result += fraction2rmb(fraction).substring(1); //       [ ]
        } else {
            result += fraction2rmb(fraction); //     
        }
 
        return result;
    }
 
    //               
    private static String fraction2rmb(String fraction) {
        char jiao = fraction.charAt(0); //  
        char fen = fraction.charAt(1); //  
        return (RMB_NUMS[jiao - '0'] + (jiao > '0' ? UNITS[1] : ""))
                + (fen > '0' ? RMB_NUMS[fen - '0'] + UNITS[2] : "");
    }
 
    //               
    private static String integer2rmb(String integer) {
        StringBuilder buffer = new StringBuilder();
        //         
        int i, j;
        for (i = integer.length() - 1, j = 0; i >= 0; i--, j++) {
            char n = integer.charAt(i);
            if (n == '0') {
                //   n   0   n         0  ,  [ ]
                if (i < integer.length() - 1 && integer.charAt(i + 1) != '0') {
                    buffer.append(RMB_NUMS[0]);
                }
                //   [ ]  [ ]
                if (j % 4 == 0) {
                    if (i > 0 && integer.charAt(i - 1) != '0' || i > 1 && integer.charAt(i - 2) != '0'
                            || i > 2 && integer.charAt(i - 3) != '0') {
                        buffer.append(U2[j / 4]);
                    }
                }
            } else {
                if (j % 4 == 0) {
                    buffer.append(U2[j / 4]); //   [ ]  [ ]
                }
                buffer.append(U1[j % 4]); //   [ ]、[ ] [ ]
                buffer.append(RMB_NUMS[n - '0']); //     
            }
        }
        return buffer.reverse().toString();
    }
}

3.工具類使用
 public static void main(String[] args) {
        System.out.println(ChineseYuanUtil.convert("16,409.02"));
    }

4.結果

            

5.終了
Always keep the faith!!!