金额中文转换

public class CaseUtils {

 

    private static String[] CNumber = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
    private static String[] CUnit = {"分","角","整","元","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟"};
    public static String numToChinese(String input) {
        String temp = "";
        String result = "";
        if (input == null||input.trim().length()==0){
            return "输入字串不是数字串只能包括以下字符('0'~'9','.'),输入字串最大只能精确到仟亿,小数点只能两位!";
        }
        temp = input.trim();
        try {
            Float.parseFloat(temp);
        }
        catch (Exception e) {
            return "输入字串不是数字串只能包括以下字符('0'~'9','.'),输入字串最大只能精确到仟亿,小数点只能两位!";
        }
        int len = 0;
        if (temp.indexOf(".") == -1)
            len = temp.length();
        else
            len = temp.indexOf(".");
        if (len > CUnit.length - 3) return ("输入字串最大只能精确到仟亿,小数点只能两位!");
        int n1 = 0;
        String num = "";
        String unit = "";
        for (int i = 0; i < temp.length(); i++) {
            if (i > len + 2) {
                break;
            }
            if (i == len) {
                continue;
            }
            n1 = Integer.parseInt(String.valueOf(temp.charAt(i)));
            num = CNumber[n1];
            n1 = len - i + 2;
            unit = CUnit[n1];
            result = result.concat(num).concat(unit);
        }
        if ((len == temp.length()) || (len == temp.length() - 1)) result = result.concat("整");
        if (len == temp.length() - 2) result = result.concat("零分");
        return result;
    }
}

你可能感兴趣的:(金额中文转换)