题意:
读入一个浮点数值,将其转化为中文金额的大写方式.
试验要求:
当金额为整数时,只表示整数部分,省略小数部分,并添加"整"字.
当金额中含有连续的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