码迷,mamicode.com
首页 > 其他好文 > 详细

数字转换成中文汉字数字

时间:2019-05-27 13:21:40      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:main   als   ring   new   div   i++   style   rgs   去掉   

 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 }

 

数字转换成中文汉字数字

标签:main   als   ring   new   div   i++   style   rgs   去掉   

原文地址:https://www.cnblogs.com/hxz-nl/p/10929983.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!