数字转换成中文汉字数字

Posted hxz-nl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数字转换成中文汉字数字相关的知识,希望对你有一定的参考价值。

 1 class Student {
 2     static String[] ss = new String[]{"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
 3     static String[] mm = new String[]{"仟", "佰", "拾", ""};
 4     static String[] hh = new String[]{"", "萬", "亿", "兆","京","垓"};
 5 
 6     int dd = 123456789;
 7 
 8     public static void main(String[] args) {
 9         read("22222222222222222222222");
10     }
11 
12     private static void read(String num) {
13         String[] strings = num.split("\\.");
14         String integer = strings[0];
15         String zhengs =readInteger(integer);
16         String xiaos="";
17         if(strings.length==2){
18             String decimal = strings[1];
19              xiaos="点"+readDecimal(decimal);
20         }
21         String xx=zhengs+xiaos;
22         System.out.println(xx);
23     }
24 
25     private static String readInteger(String integer) {
26         int mark = integer.length();
27         int star = mark - 4 < 0 ? 0 : mark - 4;
28         int end = mark;
29         StringBuilder stringBuilder = new StringBuilder();
30         for (int i = 0; i <= integer.length() / 4; i++) {
31             String nn = integer.substring(star, end);
32             mark=star;
33             star = mark - 4 < 0 ? 0 : mark - 4;
34             end = mark;
35             String nn1 = readfour(nn);
36             if (!nn1.equals("")) {
37                 stringBuilder = stringBuilder.insert(0,nn1 + hh[i]);
38             }
39 
40         }
41         if(stringBuilder.charAt(0) == ‘零‘){
42             stringBuilder.replace(0, 1, "");
43         }
44         return stringBuilder.toString();
45     }
46 
47     private static String readDecimal(String decimal) {
48         StringBuilder stringBuilder = new StringBuilder();
49         for (int i = 0; i < decimal.length(); i++) {
50             stringBuilder.append(ss[Integer.parseInt(decimal.charAt(i) + "")]);
51         }
52         return stringBuilder.toString();
53     }
54 
55     private static String readfour(String nn) {
56         StringBuilder stringBuilder = new StringBuilder();
57         int d=4-nn.length();
58         if(d!=0){
59             for (int i = 0; i <d ; i++) {
60                 nn="0"+nn;
61             }
62         }
63         for (int i = 0; i < nn.length(); i++) {
64             if (nn.charAt(i) == ‘0‘) {
65                 stringBuilder.append(ss[Integer.parseInt(nn.charAt(i) + "")]);
66             } else
67                 stringBuilder.append(ss[Integer.parseInt(nn.charAt(i) + "")] + mm[i]);
68         }
69 
70         //多个零变一个零
71         for (int i = 0; i < stringBuilder.length(); i++) {
72             if (stringBuilder.charAt(i) == ‘零‘) {
73                 if((i+1)==stringBuilder.length())
74                     continue;
75                 if (stringBuilder.charAt(i + 1) == ‘零‘) {
76                     stringBuilder.replace(i + 1, i + 2, "");
77                     i=i-1;
78                 }
79             }
80         }
81         //去掉末尾的零
82         if (stringBuilder.charAt(stringBuilder.length() - 1) == ‘零‘) {
83             stringBuilder.replace(stringBuilder.length() - 1, stringBuilder.length(), "");
84         }
85 
86         return stringBuilder.toString();
87     }
88 
89 
90 }

 

以上是关于数字转换成中文汉字数字的主要内容,如果未能解决你的问题,请参考以下文章

中文的数字代码哪里可以查询?

数字转换成中文汉字数字

ASP.NET 几种编码转换成汉字

数字转换成中文汉字

java实现数字金额转换成汉字大写金额

Python 将中文字母转成数字