服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java应用: 浮点数转化为大写中文金额

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

试验要求:

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

当金额中含有连续的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}

(t006)


扫描关注微信公众号