目 录CONTENT

文章目录

阿拉伯数字转中文数字字符,最高支持千京

林汐~
2023-08-10 / 0 评论 / 0 点赞 / 36 阅读 / 661 字 / 正在检测是否收录...

直接上代码


@UtilityClass
public class NumberFormatUtil {

    /** 中文 -> 数字对应关系 */
    private static final Map<Character, Integer> DIGIT_CHINA = new HashMap<>();
    /** 数字 -> 中文对应关系 */
    private static final Map<Integer, Character> DIGIT_NUM = new HashMap<>();
    /** 中文 -> 增量对应关系 */
    private static final Map<Character, Integer> POSITION_CHINA = new HashMap<>();
    /** 增量 -> 中文对应关系 */
    private static final Map<Integer, Character> POSITION_NUM = new HashMap<>();

    static {
        DIGIT_CHINA.put('零', 0);
        DIGIT_CHINA.put('一', 1);
        DIGIT_CHINA.put('二', 2);
        DIGIT_CHINA.put('三', 3);
        DIGIT_CHINA.put('四', 4);
        DIGIT_CHINA.put('五', 5);
        DIGIT_CHINA.put('六', 6);
        DIGIT_CHINA.put('七', 7);
        DIGIT_CHINA.put('八', 8);
        DIGIT_CHINA.put('九', 9);
        DIGIT_NUM.put(0, '零');
        DIGIT_NUM.put(1, '一');
        DIGIT_NUM.put(2, '二');
        DIGIT_NUM.put(3, '三');
        DIGIT_NUM.put(4, '四');
        DIGIT_NUM.put(5, '五');
        DIGIT_NUM.put(6, '六');
        DIGIT_NUM.put(7, '七');
        DIGIT_NUM.put(8, '八');
        DIGIT_NUM.put(9, '九');
        POSITION_CHINA.put('十', 1);
        POSITION_CHINA.put('百', 2);
        POSITION_CHINA.put('千', 3);
        POSITION_CHINA.put('万', 4);
        POSITION_CHINA.put('亿', 7);
        POSITION_CHINA.put('兆', 11);
        POSITION_CHINA.put('京', 15);
        POSITION_CHINA.put('%', -2);
        POSITION_CHINA.put('‰', -3);
        POSITION_NUM.put(1, '十');
        POSITION_NUM.put(2, '百');
        POSITION_NUM.put(3, '千');
        POSITION_NUM.put(4, '万');
        POSITION_NUM.put(8, '亿');
        POSITION_NUM.put(12, '兆');
        POSITION_NUM.put(16, '京');
        POSITION_NUM.put(-2, '%');
        POSITION_NUM.put(-3, '‰');
    }

    /**
     * 将阿拉伯数字转为中文数字
     * 采用StringBuilder,连接所有数字
     * 一开始判断有无负号,小数点,以及百分号
     * 等到数字完全连接成功,再将数字和负号小数点百分号标识连接起来
     * @param number 数字
     * @return 中文字符串
     */
    public static String numberToChina(Long number) {
        if (number == 0) {
            return String.valueOf(DIGIT_NUM.get(0));
        }

        StringBuilder result = new StringBuilder();
        if (number < 0) {
            result.append("负");
            number = -number;
        }

        int position = 0;
        while (number > 0) {
            int digit = (int) (number % 10);
            Character positionNum = POSITION_NUM.get(position);
            if (digit != 0) {
                if ((position == 1 || position > 4) && digit == 1 && number < 10) {
                    // 双位数十的特殊处理
                    // 十万,十亿,十兆的特殊处理
                    result.insert(0, POSITION_NUM.get(position % 4));
                } else if (positionNum != null) {
                    // 个十百千万亿兆的特殊处理
                    result.insert(0, DIGIT_NUM.get(digit));
                    result.insert(1, positionNum);
                } else {
                    // 非1的所有数字处理
                    result.insert(0, DIGIT_NUM.get(digit));
                    if (position > 0) {
                        int pos = position > 4 ? position % 4 : position;
                        result.insert(1, POSITION_NUM.get(pos));
                    }
                }
            } else if (positionNum != null && position > 3) {
                result.insert(0, positionNum);
            } else {
                result.insert(0, DIGIT_NUM.get(digit));
            }
            number /= 10;
            position++;
        }

        return result.toString();
    }

    /**
     * 中文转换成数字
     * @param numStr 数字字符串
     * @return 数字
     */
    public static Integer chinaToNumber(String numStr) {
        throw new IllegalArgumentException(numStr);
    }
}

0

评论区