JAVA應用: 浮點數轉化為大寫中文金額

來源:互聯網
上載者:User

讀入一個浮點數值,將其轉化為金額的中文大寫方式.

 

實驗要求:

 

當金額為整數時,只表示整數部分,省略小數部分,並添加"整"字.

 

當金額中含有連續的0時,只需要一個"零"即可.

 

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

 

 

--------------------------------------------------------------------------------

1import java.io.*;

2class chineseMoney...{

3 private String number[]=...{"","壹","貳","三","肆","伍","陸","柒","捌","玖"};

4 private String unit[]=...{"","拾","佰","仟"};

5 private String small[]=...{"角","分"};

6 //private String strNumber,strUnit,strAll;

7

8 //是否在number中

9 private boolean IsInNumber(String strNumber)

10 ...{

11 boolean inNumber=false;

12 for (int i=0;i<9;i++)

13 ...{

14 if (strNumber.compareTo (number[i])==0) inNumber=true;

15 }

16 return inNumber;

17 }

18

19

20 private String SplitChineseNumber(int intUnit,String strInt)

21 ...{

22 int l=strInt.length ();

23 int j,k,zeorCountTemp=0;

24 String strUnit="",strNumber="",strAll="";

25

26 //判斷在千萬到萬位 是否全為0,是的話,不返回“萬”,返回“”;

27 boolean temp=false;

28 for (k=0;k29 ...{

30 String strTemp=strInt.substring(k,k+1);

31 int intTemp=Integer.parseInt(strTemp);

32

33 if (intTemp!=0) temp=true;

34 }

35 if (temp==false)

36 ...{

37 if (intUnit==5)return "";

38 }

39

40

41 int checkK=0;

42 //正式開始轉換

43 for (k=0;k44 ...{

45 String strTemp=strInt.substring(k,k+1);

46 int intTemp=Integer.parseInt(strTemp);

47 strNumber= number[intTemp];

48

49 //j 從

50 j=l-1-k;

51

52 strUnit=unit[j];

53

54

55 //數值+單位

56 //如果數值=0,數值=“”

57 if (intTemp==0)

58 ...{

59 //

60 if (zeorCountTemp==0)

61 ...{

62 //單位=零

63 strUnit=strUnit.replace(/'/'/'/'拾/'/'/'/',/'/'/'/'零/'/'/'/');

64 strUnit=strUnit.replace(/'/'/'/'佰/'/'/'/',/'/'/'/'零/'/'/'/');

65 strUnit=strUnit.replace(/'/'/'/'仟/'/'/'/',/'/'/'/'零/'/'/'/');

66 }

67 else

68 ...{

69 //多零情況下,單位=“”

70 strUnit=strUnit.replaceAll("拾","");

71 strUnit=strUnit.replaceAll("佰","");

72 strUnit=strUnit.replaceAll("仟","");

73 }

74 zeorCountTemp++;

75 }

76 checkK=k;

77 strAll+=strNumber+strUnit;

78 }

79

80 return strAll;

81 }

82

83 private String onlyInt(int intInt)

84 ...{

85 String strInt;

86 strInt=String.valueOf(intInt);

87 int l=strInt.length();

88

89 String strAll="";

90 //按照四位 一分隔 來計算

91 if (l>8)//億

92 ...{

93 strAll+=this.SplitChineseNumber(9,strInt.substring(0,l-8))+"億";

94 strAll+=this.SplitChineseNumber(5,strInt.substring(l-8,l-4));

95 strAll+=this.SplitChineseNumber(1,strInt.substring(l-4,l))+"元";

96 }

97 else if (l>4)//萬

98 ...{

99 strAll+=this.SplitChineseNumber(5,strInt.substring(0,l-4));

100 strAll+=this.SplitChineseNumber(1,strInt.substring(l-4,l))+"元";

101

102 }

103 else if (l>0)

104 ...{

105 strAll+=this.SplitChineseNumber(1,strInt)+"元";

106 }

107//

108//

109//

110//

111// 100101000

112 int checkL=strAll.length();

113

114 char strTemp2;

115 for (int k=1;k116 ...{

117 strTemp2=strAll.charAt(k);

118 if (strTemp2==/'/'/'/'零/'/'/'/')

119 ...{

120 //判斷零的前後是否有數字,無數字則刪除這個零

121 String strBeforeTemp=strAll.substring(k-1,k);

122 String strAfterTemp=strAll.substring(k+1,k+2);

123 if (!this.IsInNumber(strBeforeTemp)&&!this.IsInNumber(strAfterTemp))

124 ...{

125 strBeforeTemp=strAll.substring(0,k);

126 strAfterTemp=strAll.substring(k+1,checkL);

127 strAll= strBeforeTemp+strAfterTemp;

128 break;

129 }

130

131 }

132 }

133

134 return strAll;

135

136 }

137

138 private String onlySmall(int intSmall)

139 ...{

140 String strNumber,strUnit,strAll;

141 strNumber="";strUnit="";strAll="";

142 String strSmall,strTemp;

143 strSmall=String.valueOf(intSmall);

144 int i;

145 if (intSmall>=10)

146 ...{

147 for (i=0;i148 ...{

149 strTemp=String.valueOf(intSmall).substring(i,i+1);

150 if (Integer.parseInt(strTemp)!=0)

151 ...{

152 strNumber=number[Integer.parseInt(strTemp)];

153 strUnit=small[i];

154 strAll+=strNumber+strUnit;

155 }

156 }

157 }

158 else

159 ...{

160 if (intSmall!=0)

161 ...{

162 strNumber=number[intSmall];

163 strUnit=small[1];

164 strAll+=strNumber+strUnit;

165 }

166 }

167

168 return strAll;

169 }

170

171 public String getChineseMoney(double number)

172 ...{

173 //四捨五入

174 number=(number*100+0.5)/100;

175

176 String strAll,strChineseInt,strChineseSmall,strZheng;;

177 int intInt,intSmall;

178 strChineseInt="";strChineseSmall="";strZheng="";

179

180 //整數部分

181 intInt=(int)( number*100/100);

182 if (intInt!=0)

183 ...{

184 strChineseInt=onlyInt(intInt);

185 }

186 //小數部分

187 double temp=(number-intInt)*100*100/100;

188 //對小數部分四捨五入

189 intSmall=(int)(temp*100+0.5)/100;

190 if (intSmall!=0)

191 ...{

192 strChineseSmall=onlySmall(intSmall);

193 }

194 else

195 ...{

196 strZheng="整";

197 }

198 strAll=strChineseInt+strChineseSmall+strZheng;

199 return strAll;

200 }

201 public static void main(String args[]) throws IOException

202 ...{

203 chineseMoney cm=new chineseMoney();

204 double money;

205 String strMoney,strChineseMoney;

206 strMoney="";

207 //讀取

208 System.out.println("輸入貨幣(四捨五入):");

209 BufferedReader cin = new BufferedReader(new InputStreamReader( System.in));

210 strMoney = cin.readLine();

211 money=Double.parseDouble(strMoney);

212 //money=12346.465;//Double.parseDouble(strMoney);

213 strChineseMoney=cm.getChineseMoney(money);

214 System.out.println(strChineseMoney);

215 }

216}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.