其他分享
首页 > 其他分享> > 阿拉伯数字转中文

阿拉伯数字转中文

作者:互联网

package com.company;


import java.util.Objects;

/**
 * @Author: hugowu
 * @Description:
 * @Date: 2021/10/15 下午5:13
 */
public class MathToChinese {


    public static String mathToChinese(Long number){
        String[] arr1 = new String[]{
                "零",
                "一",
                "二",
                "三",
                "四",
                "五",
                "六",
                "七",
                "八",
                "九"
        };
        String[] arr2 = new String[]{
                "",
                "十",
                "百",
                "千",
                "万",
                "十",
                "百",
                "千",
                "亿",
                "十",
                "百",
                "千",
                "万",
                "十",
                "百",
                "千",
                "亿"
        };
        //可继续追加更高位转换值
        if (Objects.isNull(number)) {
            return "零";
        }
        String[] english = number.toString().split("");
        String result = "";
        for (int i = 0; i < english.length; i++) {
            int num = english.length - 1 - i;//倒序排列设值
            result = arr2[i] + result;
            String str = english[num];
            result = arr1[Integer.parseInt(str)] + result;
        }

        result = result.replaceAll("零[零千百十]", "零");
        result = result.replaceAll("零([万亿])", "$1零");
        result = result.replaceAll("亿万", "亿");
        result = result.replaceAll("兆[万]", "兆");
        result = result.replaceAll("一十", "十");
        //result = result.replaceAll("零圆", "圆");


      /*  //将【零千、零百】换成【零】 【十零】换成【十】
        result = result.replaceAll()"零(?=千|百|十), "零").replace(/十零/g, "十");
        //合并中间多个零为一个零
        result = result.replaceAll(/零+/g, "零");
        //将【零亿】换成【亿】【零万】换成【万】
        result = result.replaceAll(/零亿/g, "亿").replace(/零万/g, "万");
        //将【亿万】换成【亿】
        result = result.replaceAll(/亿万/g, "亿");
        //移除末尾的零
        //result = result.replace(/零+$/, "");
        //将【零一十】换成【零十】
        //result = result.replace(/零一十/g, '零十');//貌似正规读法是零一十
        //将【一十】换成【十】
        result = result.replaceAll(^"一十", "十");*/
        return result;
    }

    public static void main(String[] args) {
        System.out.println(mathToChinese(152465478534535L));
    }
}

经测试:
一百五十二万四千六百五十四亿七千八百五十三万四千五百三十五

Process finished with exit code 0

标签:中文,一十,String,阿拉伯数字,replace,replaceAll,换成,result
来源: https://blog.csdn.net/xiao_xiao3601/article/details/120827404