[收藏]JAVA中金额的中文大写方式

Posted on 2006-04-02 15:34 ikingqu 阅读(705) 评论(1)  编辑  收藏 所属分类: JavaSE&JavaEE

题意:

读入一个浮点数值,将其转化为中文金额的大写方式.

试验要求:

当金额为整数时,只表示整数部分,省略小数部分,并添加"整"字.

当金额中含有连续的0时,只需要一个"零"即可.

10的表示方式.例如110--壹佰一拾元整,10---一拾元整

  1 import java.io. * ;
  2 class chineseMoney {
  3     private String number[] = { "" , " " , " " , " " , " " , " " , " " , " " , " " , " " } ;
  4     private String unit[] = { " " , " " , " " , " " , " " , " " , " " , " " , " 亿 " , " " , " " } ;
  5     private String small[] = { " " , " " } ;
  6     private String strNumber,strUnit,strAll;
  7
  8     private String onlyInt( int intInt)
  9     {
10         String strInt;
11         strInt = String.valueOf(intInt);
12         strNumber = "" ;strUnit = "" ;strAll = "" ;
13         int l = strInt.length ();
14         int j,k,zeorCount;
15         zeorCount = 0 ;
16         for (k = 0 ;k < l;k ++ )
17         {
18             String strTemp = strInt.substring(k,k + 1 );
19             int intTemp = Integer.parseInt(strTemp);
20             strNumber = number[intTemp];
21             j = l - 1 - k;
22             strUnit = unit[j];
23             if (intTemp == 0 )
24             {
25                 if (zeorCount == 0 )
26                 {
27                     strUnit = strUnit.replace( ' ' , ' ' );
28                     strUnit = strUnit.replace( ' ' , ' ' );
29                     strUnit = strUnit.replace( ' ' , ' ' );
30                     strUnit = strUnit.replace( ' ' , ' ' );   
31                 }

32                 else
33                 {
34                     strUnit = strUnit.replaceAll( " " , "" );
35                     strUnit = strUnit.replaceAll( " " , "" );
36                     strUnit = strUnit.replaceAll( " " , "" );
37                     strUnit = strUnit.replaceAll( " " , "" );       
38                 }

39                 zeorCount ++ ;
40             }
       
41             strAll += strNumber + strUnit;
42         }

43         return strAll;
44    
45     }

46    
47     private String onlySmall( int intSmall)
48     {
49        
50         strNumber = "" ;strUnit = "" ;strAll = "" ;
51         String strSmall,strTemp;
52         strSmall = String.valueOf(intSmall);
53         int i;
54         if (intSmall >= 10 )
55         {
56             for (i = 0 ;i < strSmall.length();i ++ )
57             {
58                 strTemp = String.valueOf(intSmall).substring(i,i + 1 );
59                 if (Integer.parseInt(strTemp) != 0 )
60                 {
61                     strNumber = number[Integer.parseInt(strTemp)];
62                     strUnit = small[i];   
63                     strAll += strNumber + strUnit;
64                 }

65             }

66         }

67         else
68         {
69             if (intSmall != 0 )
70             {
71                 strNumber = number[intSmall];
72                 strUnit = small[ 1 ];   
73                 strAll += strNumber + strUnit;
74             }

75         }

76
77         return strAll;
78     }

79    
80     public String getChineseMoney( double number)
81     {
82         // 四舍五入
83         number = (number * 100 + 0.5 ) / 100 ;
84        
85         String strAll,strChineseInt,strChineseSmall,strZheng;;
86         int intInt,intSmall;
87         strChineseInt = "" ;strChineseSmall = "" ;strZheng = "" ;
88        
89         // 整数部分
90         intInt = ( int )( number * 100 / 100 );
91         if (intInt != 0 )
92         {
93             strChineseInt = onlyInt(intInt);
94         }

95         // 小数部分
96         double temp = (number - intInt) * 100 * 100 / 100 ;
97         // 对小数部分四舍五入
98         intSmall = ( int )(temp * 100 + 0.5 ) / 100 ;
99         if (intSmall != 0 )
100         {
101             strChineseSmall = onlySmall(intSmall);
102         }

103         else
104         {
105             strZheng = " " ;
106         }

107         strAll = strChineseInt + strChineseSmall + strZheng;
108         return strAll;
109     }

110     public static void main(String args[]) throws IOException
111     {
112         chineseMoney cm = new chineseMoney();
113         double money;
114         String strMoney,strChineseMoney;
115         strMoney = "" ;
116         // 读取
117         System.out.println( " 输入货币(四舍五入): " );
118         BufferedReader cin = new BufferedReader( new InputStreamReader( System.in));
119         strMoney = cin.readLine();
120         money = Double.parseDouble(strMoney);
121         strChineseMoney = cm.getChineseMoney(money);
122         System.out.println(strChineseMoney);
123     }

124 }



 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=641419

Feedback

# re: [收藏]JAVA中金额的中文大写方式  回复  更多评论   

2011-02-18 09:58 by oplife@163.com
这个有个地方不太对,当金额为800.00时,输出的是:
捌佰零圆整

多了个零

只有注册用户登录后才能发表评论。


网站导航:
 

posts - 4, comments - 5, trackbacks - 0, articles - 60

Copyright © ikingqu