将数字转换为中文

来源:互联网 发布:知乎 回复评论 编辑:程序博客网 时间:2024/05/20 22:27
 import java.io.BufferedReader;  
3.import java.io.IOException;  
4.import java.io.InputStreamReader;  
5.//最高支持12位千亿位 可以扩展但没有必要。  
6.public class Number2Chinese {  
7.   public static void main(String [] arg) throws IOException{  
8.            //得到键盘输入的值  
9.            BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));  
10.            String str = buf.readLine();  
11.            Number2Chinese num = new Number2Chinese();  
12.           String str2 = num.change(str);  
13.           System.out.println(num.init(str2));  
14.     }  
15.   public  String init(String str){  
16.         str=str.replace('0', '零');  
17.         str=str.replace('1', '一');  
18.         str=str.replace('2', '二');  
19.         str=str.replace('3', '三');  
20.         str=str.replace('4', '四');  
21.         str=str.replace('5', '五');  
22.         str=str.replace('6', '六');  
23.         str=str.replace('7', '七');  
24.         str=str.replace('8', '八');  
25.         str=str.replace('9', '九');  
26.         str=str.replace("零零", "零");  
27.        return str;  
28.     }  
29.   public  static String  sub(String str){//获取每四位的子串  
30.     StringBuffer stb = new StringBuffer();  
31.     int leng = str.length();  
32.         if(leng%4==1){  
33.             stb.append(str.charAt(0));  
34.         }  
35.         if(leng%4==2){  
36.             stb.append(str.charAt(0)+"拾"+(str.charAt(1)=='0'?"":str.charAt(1)));  
37.         }  
38.         if(leng%4==3){  
39.             stb.append(  
40.                    str.charAt(0)+"佰" 
41.                    +(str.charAt(1)=='0'?(str.charAt(2)=='0'?"":'零'):(str.charAt(1)+"拾"))  
42.                    +(str.charAt(2)=='0'?"":str.charAt(2)));  
43.         }  
44.     if(leng%4==0){   
45.         if("0000".equals(str))stb.append("");  
46.         if(str.charAt(0)=='0'){stb.append( ""+  
47.           (str.charAt(1)=='0'?(str.charAt(2)=='0'?(str.charAt(3)=='0'?"":'零'):'零'):(str.charAt(1)+"佰"))  
48.            +(str.charAt(2)=='0'?(str.charAt(3)=='0'?"":'零'):(str.charAt(2)+"拾"))  
49.            +(str.charAt(3)=='0'?"":str.charAt(3)));}  
50.         else stb.append(  
51.            str.charAt(0)+"仟" 
52.            + (str.charAt(1)=='0'?(str.charAt(2)=='0'?(str.charAt(3)=='0'?"":'零'):'零'):(str.charAt(1)+"佰"))  
53.            +(str.charAt(2)=='0'?(str.charAt(3)=='0'?"":'零'):(str.charAt(2)+"拾"))  
54.            +(str.charAt(3)=='0'?"":str.charAt(3)));  
55.     }  
56.         return stb.toString();  
57.     }  
58.  public  String change(String str){  
59.         StringBuffer strbuf = new StringBuffer();  
60.         int len = str.length();  
61.            if(len>8){  
62.                String a = str.substring(0,len-8);  
63.                String ag = Number2Chinese.sub(a);  
64.                strbuf.append(ag+"亿");  
65.            }  
66.            if(len>4){  
67.                String b = str.substring(len>8?len-8:0,len-4);  
68.                String bg = Number2Chinese.sub(b);  
69.                if(!"0000".equals(b))strbuf.append(bg+"万");  
70.                else strbuf.append("");  
71.            }  
72.             String c = str.substring(len-4>0?len-4:0,len);  
73.             String cg = Number2Chinese.sub(c);  
74.             strbuf.append(cg);  
75.         return strbuf.toString();  
76.     }  
77.}