题意:
读入一个浮点数值,将其转化为中文金额的大写方式.
试验要求:
当金额为整数时,只表示整数部分,省略小数部分,并添加"整"字.
当金额中含有连续的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
//是否在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;k<l;k++)29
...{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;k<l;k++)44
...{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
else68
...{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
// 100101000112
int checkL=strAll.length();113
114
char strTemp2;115
for (int k=1;k<checkL;k++)116
...{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;i<strSmall.length();i++)148
...{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
else159
...{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
else195
...{196
strZheng="整";197
}198
strAll=strChineseInt+strChineseSmall+strZheng;199
return strAll;200
}201
public static void main(String args[]) throws IOException202
...{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
}