java把数字转换成汉字 java 数字转汉字

使用java将数字转化为中文汉字_java数字转中文_javaerly的博客-CSDN博客

package com.unicom.apartment.utils;

public class NumUtil {

    public static String convert(int number) {
        if(number <= 0){
            return "";
        }
        if(number == 1){
            return "当天";
        }
        //数字对应的汉字
        String[] num = {"一", "二", "三", "四", "五", "六", "七", "八", "九"};
        //单位
        String[] unit = {"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千", "万亿"};
        //将输入数字转换为字符串
        String result = String.valueOf(number);
        //将该字符串分割为数组存放
        char[] ch = result.toCharArray();
        //结果 字符串
        String str = "";
        int length = ch.length;
        for (int i = 0; i < length; i++) {
            int c = (int) ch[i] - 48;
            if (c != 0) {
                str += num[c - 1] + unit[length - i - 1];
            }
        }
        if(number < 20 && number > 9){
            str = str.substring(1);
        }
        //System.out.println(str);
        return str + "天";
    }


    public static void main(String[] args) {


        String num0 = NumUtil.convert(0);
        String num1 = NumUtil.convert(1);
        String num2 = NumUtil.convert(11);
        String num3 = NumUtil.convert(33);
        String num5 = NumUtil.convert(90);

        for(int i = 0 ; i <= 90 ; i++){
            String day = NumUtil.convert(i);
            System.out.println(day);
        }

        System.out.println();



    }





}

猜你喜欢

转载自blog.csdn.net/qq_27327261/article/details/132365971
今日推荐