[Java][工具方法]阿拉伯数字转中文简体和繁体

2021-09-28 1110点热度 0人点赞 0条评论

如图,分享一个我项目中自用的数字转换工具,将阿拉伯数字转成中文,例如数据库里存的是期刊1或者10,而拿出来显示的时候,是显示第十期。

这是从网站找来的,经过我自己修改后的代码:

NumberConvertUtils.java

/**
 * 数字转换工具类
 *
 * @author rubekid
 */
public class NumberConvertUtils {
    private final static String[] SIMPLIFIED_GROUP_UNITS = {"", "万", "亿", "兆"};
    private final static String[] TRADITIONAL_GROUP_UNITS = {"", "萬", "億", "兆"};
    private final static String[] SIMPLIFIED_BASE_UNITS = {"", "十", "百", "千"};
    private final static String[] TRADITIONAL_BASE_UNITS = {"", "拾", "佰", "仟"};
    private final static char[] SIMPLIFIED_NUMBERIC = {'零', '一', '二', '三', '四', '五', '六', '七', '八', '九'};
    private final static char[] TRADITIONAL_NUMBERIC = {'零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'};

    /**
     * 转为中文数字
     *
     * @param number        数值
     * @param isTraditional 是否转为繁体
     * @return
     */
    public static String toChinese(long number, boolean isTraditional) {
        char[] numberic = isTraditional ? TRADITIONAL_NUMBERIC : SIMPLIFIED_NUMBERIC;
        String[] baseUnits = isTraditional ? TRADITIONAL_BASE_UNITS : SIMPLIFIED_BASE_UNITS;

        if (number == 0) {//判断是否为0
            return String.valueOf(numberic[0]);
        }

        String str = String.valueOf(number);
        char[] numbers = str.toCharArray();
        int len = str.length();
        //统计连续出现的0
        int zeroCount = 0;

        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < len; i++) {
            int value = Integer.parseInt(String.valueOf(numbers[i]));
            int index = len - i - 1;
            //在分组中的下标
            int _index = index % 4;
            if (value == 0) {
                zeroCount++;
                if ((_index == 0 && zeroCount < 4) || (index % 8 == 0 && zeroCount < 8)) {
                    stringBuffer.append(getGroupUnit(index, isTraditional));
                    zeroCount = 0;
                }
            } else {
                if (zeroCount > 0) {
                    stringBuffer.append(numberic[0]);
                }
                if (_index == 0) {
                    stringBuffer.append(numberic[value]).append(baseUnits[_index]).append(getGroupUnit(index, isTraditional));
                    zeroCount = 0;
                } else {
                    //调整以“一十”开头的不需要出现“一” 如:100000 读“十万”而不是“一十万”
                    if (_index == 1 && i == 0 && value == 1) {
                        stringBuffer.append(baseUnits[_index]);
                    } else {
                        stringBuffer.append(numberic[value]).append(baseUnits[_index]);
                    }

                }
                zeroCount = 0;
            }

        }
        return stringBuffer.toString();
    }

    /**
     * 转为中文数字
     */
    public static String toChinese(long number) {
        return toChinese(number, false);
    }

    /**
     * 获取分组单位
     *
     */
    private static String getGroupUnit(int index, boolean isTraditional) {
        String[] groupUnits = isTraditional ? TRADITIONAL_GROUP_UNITS : SIMPLIFIED_GROUP_UNITS;
        String groupUnit = groupUnits[(index / 4) % 2];
        if (index % 8 == 0 && index > 0) {
            groupUnit += groupUnits[(index / 8) + 1];
        }
        return groupUnit;
    }
}

测试:

public static void main(String[] args) throws Exception {
    System.out.println(NumberConvertUtils.toChinese(0)); //零
    System.out.println(NumberConvertUtils.toChinese(10, false)); //十
    System.out.println(NumberConvertUtils.toChinese(99, true)); //玖拾玖
    System.out.println(NumberConvertUtils.toChinese(1010)); //一千零一十
}

 

admin

这个人很懒,什么都没留下

文章评论

您需要 登录 之后才可以评论