//
UI方式
import
javax.swing.JOptionPane;
public
class
ChinaChar
{
static
char
pp(
char
c)
{
switch
(c)
{
case
'
1
'
:
return
'
壹
'
;
case
'
2
'
:
return
'
贰
'
;
case
'
3
'
:
return
'
叁
'
;
case
'
4
'
:
return
'
肆
'
;
case
'
5
'
:
return
'
伍
'
;
case
'
6
'
:
return
'
陆
'
;
case
'
7
'
:
return
'
柒
'
;
case
'
8
'
:
return
'
捌
'
;
case
'
9
'
:
return
'
玖
'
;
case
'
0
'
:
return
'
零
'
;
}
return
c;
}
static
StringBuffer ZhuanHuan(String iin)
{
String in
=
iin;
boolean
xb
=
false
;
//
默认没有小数
if
(in.indexOf(
"
.
"
)
!=
-
1
)
{
xb
=
true
;
}
//
如果有小数点就将其按小数点分割,并分别存入s1和s2中
StringBuffer out
=
new
StringBuffer(
""
);
String s1
=
new
String(
""
);
String s2
=
new
String(
""
);
if
(in.indexOf(
'
.
'
)
!=
-
1
)
{
in
=
in.replace(
'
.
'
,
'
#
'
);
String[] s
=
in.split(
"
#
"
);
s1
=
s[
0
];
s2
=
s[
1
];
}
else
{
s1
=
in;
}
//
输出小数点左边的数
if
(s1.length()
>=
9
)
{
//
亿
String yi
=
s1.substring(
0
, s1.length()
-
8
);
String wan
=
s1.substring(s1.length()
-
8
, s1.length()
-
4
);
String qian
=
s1.substring(s1.length()
-
4
, s1.length());
int
len
=
yi.length();
boolean
b
=
true
;
//
处理亿区间
for
(
int
i
=
0
; i
<
yi.length(); i
++
)
{
if
(len
==
4
)
{
out.append(ChinaChar.pp(yi.charAt(i)));
out.append(
"
仟
"
);
len
--
;
}
else
if
(len
==
3
)
{
if
(yi.charAt(i)
==
'
0
'
)
{
b
=
false
;
out.append(
"
零
"
);
len
--
;
continue
;
}
else
{
out.append(ChinaChar.pp(yi.charAt(i)));
out.append(
"
佰
"
);
len
--
;
continue
;
}
}
else
if
(len
==
2
)
{
if
(yi.charAt(i)
==
'
0
'
&&
!
b)
{
len
--
;
continue
;
}
if
(yi.charAt(i)
==
'
0
'
&&
b)
{
b
=
false
;
out.append(
"
零
"
);
len
--
;
continue
;
}
else
{
out.append(ChinaChar.pp(yi.charAt(i)));
out.append(
"
拾
"
);
len
--
;
continue
;
}
}
else
if
(len
==
1
)
{
if
(yi.charAt(i)
==
'
0
'
)
{
out.append(
"
亿
"
);
len
--
;
continue
;
}
else
{
out.append(ChinaChar.pp(yi.charAt(i)));
out.append(
"
亿
"
);
len
--
;
}
}
}
if
(out.charAt(out.indexOf(
"
亿
"
)
-
1
)
==
'
零
'
)
{
out.deleteCharAt(out.indexOf(
"
亿
"
)
-
1
);
out.append(
"
零
"
);
}
if
(yi.charAt(yi.length()
-
1
)
==
'
0
'
)
{
out.append(
"
零
"
);
}
if
(out.charAt(out.length()
-
2
)
==
out.charAt(out.length()
-
1
))
{
//
删除最后多余的零
out.deleteCharAt(out.lastIndexOf(
"
零
"
));
}
//
处理万区间
len
=
wan.length();
b
=
true
;
if
(wan.charAt(
0
)
==
'
0
'
&&
wan.charAt(
1
)
==
'
0
'
&&
wan.charAt(
2
)
==
'
0
'
&&
wan.charAt(
3
)
==
'
0
'
)
{
//
如果万区间全为0就什么也不做
}
else
{
for
(
int
i
=
0
; i
<
wan.length(); i
++
)
{
if
(len
==
4
)
{
if
(wan.charAt(i)
==
'
0
'
)
{
out.append(
"
零
"
);
len
--
;
continue
;
}
out.append(ChinaChar.pp(wan.charAt(i)));
out.append(
"
仟
"
);
len
--
;
}
else
if
(len
==
3
)
{
if
(wan.charAt(i)
==
'
0
'
)
{
b
=
false
;
out.append(
"
零
"
);
len
--
;
continue
;
}
else
{
out.append(ChinaChar.pp(wan.charAt(i)));
out.append(
"
佰
"
);
len
--
;
continue
;
}
}
else
if
(len
==
2
)
{
if
(wan.charAt(i)
==
'
0
'
&&
!
b)
{
len
--
;
continue
;
}
if
(wan.charAt(i)
==
'
0
'
&&
b)
{
b
=
false
;
out.append(
"
零
"
);
len
--
;
continue
;
}
else
{
out.append(ChinaChar.pp(wan.charAt(i)));
out.append(
"
拾
"
);
len
--
;
continue
;
}
}
else
if
(len
==
1
)
{
if
(wan.charAt(i)
==
'
0
'
)
{
out.append(
"
万
"
);
len
--
;
continue
;
}
else
{
out.append(ChinaChar.pp(wan.charAt(i)));
out.append(
"
万
"
);
len
--
;
}
}
}
if
(out.charAt(out.indexOf(
"
万
"
)
-
1
)
==
'
零
'
)
{
out.deleteCharAt(out.indexOf(
"
万
"
)
-
1
);
out.append(
"
零
"
);
}
if
(wan.charAt(wan.length()
-
1
)
==
'
0
'
)
{
out.append(
"
零
"
);
}
if
(out.charAt(out.length()
-
2
)
==
out
.charAt(out.length()
-
1
))
{
out.deleteCharAt(out.lastIndexOf(
"
零
"
));
}
}
//
千区间
len
=
qian.length();
b
=
true
;
if
(qian.charAt(
0
)
==
'
0
'
&&
qian.charAt(
1
)
==
'
0
'
&&
qian.charAt(
2
)
==
'
0
'
&&
qian.charAt(
3
)
==
'
0
'
)
{
//
如果千区间全为0就什么也不做
}
else
{
for
(
int
i
=
0
; i
<
qian.length(); i
++
)
{
if
(len
==
4
)
{
if
(qian.charAt(i)
==
'
0
'
)
{
out.append(
"
零
"
);
len
--
;
continue
;
}
out.append(ChinaChar.pp(qian.charAt(i)));
out.append(
"
仟
"
);
len
--
;
}
else
if
(len
==
3
)
{
if
(qian.charAt(i)
==
'
0
'
)
{
b
=
false
;
out.append(
"
零
"
);
len
--
;
continue
;
}
else
{
out.append(ChinaChar.pp(qian.charAt(i)));
out.append(
"
佰
"
);
len
--
;
continue
;
}
}
else
if
(len
==
2
)
{
if
(qian.charAt(i)
==
'
0
'
&&
!
b)
{
len
--
;
continue
;
}
if
(qian.charAt(i)
==
'
0
'
&&
b)
{
b
=
false
;
out.append(
"
零
"
);
len
--
;
continue
;
}
else
{
out.append(ChinaChar.pp(qian.charAt(i)));
out.append(
"
拾
"
);
len
--
;
continue
;
}
}
else
if
(len
==
1
)
{
if
(qian.charAt(i)
==
'
0
'
)
{
out.append(
"
圆
"
);
len
--
;
continue
;
}
else
{
out.append(ChinaChar.pp(qian.charAt(i)));
out.append(
"
圆
"
);
len
--
;
}
}
}
}
if
(out.charAt(out.length()
-
2
)
==
'
零
'
)
{
out.deleteCharAt(out.length()
-
2
);
}
if
(out.charAt(out.length()
-
1
)
==
'
零
'
)
{
//
删除多余的零
while
(out.charAt(out.length()
-
1
)
==
'
零
'
)
{
out.deleteCharAt(out.length()
-
1
);
}
out.append(
"
圆
"
);
}
for
(
int
i
=
out.length()
-
1
; i
>=
0
; i
--
)
{
//
删除多余的零
if
(out.indexOf(
"
零
"
)
!=-
1
)
{
if
(out.charAt(out.lastIndexOf(
"
零
"
)
-
1
)
==
'
零
'
)
{
out.deleteCharAt(out.lastIndexOf(
"
零
"
)
-
1
);
}
}
}
//
处理小数
if
(xb)
{
int
llen
=
s2.length();
if
(llen
>=
2
)
{
if
(s2.charAt(
0
)
==
'
0
'
&&
s2.charAt(
1
)
==
'
0
'
)
{
//
小数为两个0就什么也不做
}
else
if
(s2.charAt(
1
)
==
'
0
'
)
{
out.append(ChinaChar.pp(s2.charAt(
0
)));
out.append(
"
角
"
);
}
else
if
(s2.charAt(
0
)
==
'
0
'
)
{
out.append(
"
零
"
);
out.append(ChinaChar.pp(s2.charAt(
1
)));
out.append(
"
分
"
);
}
else
{
out.append(ChinaChar.pp(s2.charAt(
0
)));
out.append(
"
角
"
);
out.append(ChinaChar.pp(s2.charAt(
1
)));
out.append(
"
分
"
);
}
}
if
(llen
==
1
)
{
if
(s2.charAt(
0
)
==
'
0
'
)
{
//
小数为两个0就什么也不做
}
else
{
out.append(ChinaChar.pp(s2.charAt(
0
)));
out.append(
"
角
"
);
}
}
return
out;
}
else
{
return
out;
}
}
else
if
(s1.length()
>=
5
)
{
//
万
String wan
=
s1.substring(
0
, s1.length()
-
4
);
String qian
=
s1.substring(s1.length()
-
4
, s1.length());
//
处理万区间
int
len
=
wan.length();
boolean
b
=
true
;
if
(wan.charAt(
0
)
==
'
0
'
&&
wan.charAt(
1
)
==
'
0
'
&&
wan.charAt(
2
)
==
'
0
'
&&
wan.charAt(
3
)
==
'
0
'
)
{
//
如果万区间全为0就什么也不做
}
else
{
for
(
int
i
=
0
; i
<
wan.length(); i
++
)
{
if
(len
==
4
)
{
out.append(ChinaChar.pp(wan.charAt(i)));
out.append(
"
仟
"
);
len
--
;
}
else
if
(len
==
3
)
{
if
(wan.charAt(i)
==
'
0
'
)
{
b
=
false
;
out.append(
"
零
"
);
len
--
;
continue
;
}
else
{
out.append(ChinaChar.pp(wan.charAt(i)));
out.append(
"
佰
"
);
len
--
;
continue
;
}
}
else
if
(len
==
2
)
{
if
(wan.charAt(i)
==
'
0
'
&&
!
b)
{
len
--
;
continue
;
}
if
(wan.charAt(i)
==
'
0
'
&&
b)
{
b
=
false
;
out.append(
"
零
"
);
len
--
;
continue
;
}
else
{
out.append(ChinaChar.pp(wan.charAt(i)));
out.append(
"
拾
"
);
len
--
;
continue
;
}
}
else
if
(len
==
1
)
{
if
(wan.charAt(i)
==
'
0
'
)
{
out.append(
"
万
"
);
len
--
;
continue
;
}
else
{
out.append(ChinaChar.pp(wan.charAt(i)));
out.append(
"
万
"
);
len
--
;
}
}
}
if
(out.charAt(out.indexOf(
"
万
"
)
-
1
)
==
'
零
'
)
{
out.deleteCharAt(out.indexOf(
"
万
"
)
-
1
);
out.append(
"
零
"
);
}
if
(wan.charAt(wan.length()
-
1
)
==
'
0
'
)
{
out.append(
"
零
"
);
}
if
(out.charAt(out.length()
-
2
)
==
out
.charAt(out.length()
-
1
))
{
out.deleteCharAt(out.lastIndexOf(
"
零
"
));
}
}
//
千区间
len
=
qian.length();
b
=
true
;
if
(qian.charAt(
0
)
==
'
0
'
&&
qian.charAt(
1
)
==
'
0
'
&&
qian.charAt(
2
)
==
'
0
'
&&
qian.charAt(
3
)
==
'
0
'
)
{
//
如果千区间全为0就什么也不做
}
else
{
for
(
int
i
=
0
; i
<
qian.length(); i
++
)
{
if
(len
==
4
)
{
if
(qian.charAt(i)
==
'
0
'
&&
out.charAt(out.length()
-
1
)
!=
'
零
'
)
{
out.append(
"
零
"
);
len
--
;
continue
;
}
else
if
(qian.charAt(i)
==
'
0
'
&&
out.charAt(out.length()
-
1
)
==
'
零
'
)
{
len
--
;
continue
;
}
out.append(ChinaChar.pp(qian.charAt(i)));
out.append(
"
仟
"
);
len
--
;
}
else
if
(len
==
3
)
{
if
(qian.charAt(i)
==
'
0
'
)
{
b
=
false
;
out.append(
"
零
"
);
len
--
;
continue
;
}
else
{
out.append(ChinaChar.pp(qian.charAt(i)));
out.append(
"
佰
"
);
len
--
;
continue
;
}
}
else
if
(len
==
2
)
{
if
(qian.charAt(i)
==
'
0
'
&&
!
b)
{
len
--
;
continue
;
}
if
(qian.charAt(i)
==
'
0
'
&&
b)
{
b
=
false
;
out.append(
"
零
"
);
len
--
;
continue
;
}
else
{
out.append(ChinaChar.pp(qian.charAt(i)));
out.append(
"
拾
"
);
len
--
;
continue
;
}
}
else
if
(len
==
1
)
{
if
(qian.charAt(i)
==
'
0
'
)
{
out.append(
"
圆
"
);
len
--
;
continue
;
}
else
{
out.append(ChinaChar.pp(qian.charAt(i)));
out.append(
"
圆
"
);
len
--
;
}
}
}
}
if
(out.charAt(out.length()
-
2
)
==
'
零
'
)
{
out.deleteCharAt(out.length()
-
2
);
}
if
(out.charAt(out.length()
-
1
)
==
'
零
'
)
{
//
删除多余的零
while
(out.charAt(out.length()
-
1
)
==
'
零
'
)
{
out.deleteCharAt(out.length()
-
1
);
}
out.append(
"
圆
"
);
}
for
(
int
i
=
out.length()
-
1
; i
>=
0
; i
--
)
{
//
删除多余的零
if
(out.indexOf(
"
零
"
)
!=-
1
)
{
if
(out.charAt(out.lastIndexOf(
"
零
"
)
-
1
)
==
'
零
'
)
{
out.deleteCharAt(out.lastIndexOf(
"
零
"
)
-
1
);
}
}
}
//
处理小数点右边的数
if
(xb)
{
int
llen
=
s2.length();
if
(llen
>=
2
)
{
if
(s2.charAt(
0
)
==
'
0
'
&&
s2.charAt(
1
)
==
'
0
'
)
{
//
小数为两个0就什么也不做
}
else
if
(s2.charAt(
1
)
==
'
0
'
)
{
out.append(ChinaChar.pp(s2.charAt(
0
)));
out.append(
"
角
"
);
}
else
if
(s2.charAt(
0
)
==
'
0
'
)
{
out.append(
"
零
"
);
out.append(ChinaChar.pp(s2.charAt(
1
)));
out.append(
"
分
"
);
}
else
{
out.append(ChinaChar.pp(s2.charAt(
0
)));
out.append(
"
角
"
);
out.append(ChinaChar.pp(s2.charAt(
1
)));
out.append(
"
分
"
);
}
}
if
(llen
==
1
)
{
if
(s2.charAt(
0
)
==
'
0
'
)
{
//
小数为两个0就什么也不做
}
else
{
out.append(ChinaChar.pp(s2.charAt(
0
)));
out.append(
"
角
"
);
}
}
return
out;
}
else
{
return
out;
}
}
else
{
//
千
String qian
=
s1;
//
千区间
int
len
=
qian.length();
boolean
b
=
true
;
if
(qian.charAt(
0
)
==
'
0
'
&&
qian.charAt(
1
)
==
'
0
'
&&
qian.charAt(
2
)
==
'
0
'
&&
qian.charAt(
3
)
==
'
0
'
)
{
//
如果千区间全为0就什么也不做
}
else
{
for
(
int
i
=
0
; i
<
qian.length(); i
++
)
{
if
(len
==
4
)
{
out.append(ChinaChar.pp(qian.charAt(i)));
out.append(
"
仟
"
);
len
--
;
}
else
if
(len
==
3
)
{
if
(qian.charAt(i)
==
'
0
'
)
{
b
=
false
;
out.append(
"
零
"
);
len
--
;
continue
;
}
else
{
out.append(ChinaChar.pp(qian.charAt(i)));
out.append(
"
佰
"
);
len
--
;
continue
;
}
}
else
if
(len
==
2
)
{
if
(qian.charAt(i)
==
'
0
'
&&
!
b)
{
len
--
;
continue
;
}
if
(qian.charAt(i)
==
'
0
'
&&
b)
{
b
=
false
;
out.append(
"
零
"
);
len
--
;
continue
;
}
else
{
out.append(ChinaChar.pp(qian.charAt(i)));
out.append(
"
拾
"
);
len
--
;
continue
;
}
}
else
if
(len
==
1
)
{
if
(qian.charAt(i)
==
'
0
'
)
{
out.append(
"
圆
"
);
len
--
;
continue
;
}
else
{
out.append(ChinaChar.pp(qian.charAt(i)));
out.append(
"
圆
"
);
len
--
;
}
}
}
}
if
(out.charAt(out.length()
-
2
)
==
'
零
'
)
{
out.deleteCharAt(out.length()
-
2
);
}
if
(out.charAt(out.length()
-
1
)
==
'
零
'
)
{
//
删除多余的零
while
(out.charAt(out.length()
-
1
)
==
'
零
'
)
{
out.deleteCharAt(out.length()
-
1
);
}
out.append(
"
圆
"
);
}
//
处理小数点右边的数
if
(xb)
{
int
llen
=
s2.length();
if
(llen
>=
2
)
{
if
(s2.charAt(
0
)
==
'
0
'
&&
s2.charAt(
1
)
==
'
0
'
)
{
//
小数为两个0就什么也不做
}
else
if
(s2.charAt(
1
)
==
'
0
'
)
{
out.append(ChinaChar.pp(s2.charAt(
0
)));
out.append(
"
角
"
);
}
else
if
(s2.charAt(
0
)
==
'
0
'
)
{
out.append(
"
零
"
);
out.append(ChinaChar.pp(s2.charAt(
1
)));
out.append(
"
分
"
);
}
else
{
out.append(ChinaChar.pp(s2.charAt(
0
)));
out.append(
"
角
"
);
out.append(ChinaChar.pp(s2.charAt(
1
)));
out.append(
"
分
"
);
}
}
if
(llen
==
1
)
{
if
(s2.charAt(
0
)
==
'
0
'
)
{
//
小数为两个0就什么也不做
}
else
{
out.append(ChinaChar.pp(s2.charAt(
0
)));
out.append(
"
角
"
);
}
}
return
out;
}
else
{
return
out;
}
}
}
}
import
java.awt.BorderLayout;
import
java.awt.LayoutManager;
import
java.awt.event.ActionEvent;
import
javax.swing.JPanel;
import
javax.swing.JFrame;
import
javax.swing.JButton;
import
java.awt.FlowLayout;
import
javax.swing.BoxLayout;
import
java.awt.CardLayout;
import
java.awt.Color;
import
java.awt.GridBagLayout;
import
java.awt.GridBagConstraints;
import
java.awt.GridLayout;
import
javax.swing.JTextField;
import
javax.swing.JTextPane;
import
javax.swing.JEditorPane;
import
javax.swing.JProgressBar;
import
java.awt.Panel;
import
java.awt.event.ActionListener;
import
java.awt.Font;
public
class
mm
extends
JFrame
{
private
JPanel jContentPane
=
null
;
private
JTextField out
=
null
;
private
JTextField err
=
null
;
private
JButton jButton
=
null
;
private
JTextField in
=
null
;
private
String ss;
/** */
/**
* This is the default constructor
*/
public
mm()
{
super
();
initialize();
}
/** */
/**
* This method initializes this
*
*
@return
void
*/
private
void
initialize()
{
this
.setSize(
536
,
199
);
this
.setResizable(
false
);
this
.setLocation(
270
,
270
);
this
.setContentPane(getJContentPane());
this
.setTitle(
"
将数字转换为中文大写
"
);
this
.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this
.setVisible(
true
);
}
/** */
/**
* This method initializes jContentPane
*
*
@return
javax.swing.JPanel
*/
private
JPanel getJContentPane()
{
if
(jContentPane
==
null
)
{
jContentPane
=
new
JPanel();
jContentPane.setLayout(
null
);
jContentPane.add(getJtf1(),
null
);
jContentPane.add(getErr(),
null
);
jContentPane.add(getJButton(),
null
);
jContentPane.add(getIn(),
null
);
}
return
jContentPane;
}
/** */
/**
* This method initializes jtf1
*
*
@return
javax.swing.JTextField
*/
private
JTextField getJtf1()
{
if
(out
==
null
)
{
out
=
new
JTextField();
out.setText(
"
NULL
"
);
out.setEditable(
false
);
out.setHorizontalAlignment(JTextField.RIGHT);
out.setLocation(
new
java.awt.Point(
28
,
131
));
out.setSize(
new
java.awt.Dimension(
462
,
31
));
out.setComponentOrientation(java.awt.ComponentOrientation.UNKNOWN);
}
return
out;
}
/** */
/**
* This method initializes err
*
*
@return
javax.swing.JTextField
*/
private
JTextField getErr()
{
if
(err
==
null
)
{
err
=
new
JTextField();
err.setHorizontalAlignment(JTextField.CENTER);
err.setEditable(
false
);
err.setBounds(
new
java.awt.Rectangle(
28
,
92
,
462
,
31
));
err.setComponentOrientation(java.awt.ComponentOrientation.UNKNOWN);
}
return
err;
}
/** */
/**
* This method initializes jButton
*
*
@return
javax.swing.JButton
*/
private
JButton getJButton()
{
if
(jButton
==
null
)
{
jButton
=
new
JButton();
jButton.setBounds(
new
java.awt.Rectangle(
200
,
49
,
124
,
32
));
jButton.setText(
"
开始转换
"
);
jButton.addActionListener(
new
Ajb(
this
));
}
return
jButton;
}
/** */
/**
* This method initializes in
*
*
@return
javax.swing.JTextField
*/
private
JTextField getIn()
{
if
(in
==
null
)
{
in
=
new
JTextField();
in.setHorizontalAlignment(JTextField.RIGHT);
in.setBounds(
new
java.awt.Rectangle(
28
,
7
,
462
,
31
));
}
return
in;
}
void
stact(ActionEvent e)
{
ss
=
in.getText();
String temp
=
ss;
Font f
=
new
Font(
"
ooo
"
,Font.BOLD,
12
);
//
出现两个小数点 或 非法字符 或 小数左边的长度大于12则报错
boolean
bb
=
true
;
for
(
int
i
=
0
; i
<
temp.length(); i
++
)
{
if
(
!
Character.isDigit(temp.charAt(i)))
{
if
(temp.charAt(i)
==
'
.
'
&&
!
bb)
{
err.setFont(f);
err.setForeground(Color.RED);
err.setText(
"
数字中出现了两个小数点!!!
"
);
return
;
}
if
(temp.charAt(i)
==
'
.
'
)
{
bb
=
false
;
}
if
(temp.charAt(i)
!=
'
.
'
)
{
err.setFont(f);
err.setForeground(Color.RED);
err.setText(
"
您输入的包含非法字符!!!
"
);
return
;
}
}
}
String s1
=
new
String(
""
);
if
(temp.charAt(
0
)
==
'
0
'
)
{
err.setFont(f);
err.setForeground(Color.RED);
err.setText(
"
整数部分第一位不能为零
"
);
return
;
}
if
(temp.indexOf(
'
.
'
)
!=
-
1
)
{
temp
=
temp.replace(
'
.
'
,
'
#
'
);
String[] s
=
temp.split(
"
#
"
);
s1
=
s[
0
];
if
(s1.length()
>
12
)
{
err.setFont(f);
err.setForeground(Color.RED);
err.setText(
"
不能转换大于千亿的数字
"
);
return
;
}
}
else
if
(temp.length()
>
12
)
{
err.setFont(f);
err.setForeground(Color.RED);
err.setText(
"
不能转换大于千亿的数字
"
);
return
;
}
//
通过数据效验就转换
StringBuffer sb
=
new
StringBuffer(ChinaChar.ZhuanHuan(ss));
err.setFont(f);
err.setForeground(Color.green);
err.setText(
"
转换成功
"
);
String oo
=
new
String(sb);
out.setText(oo);
}
class
Ajb
implements
ActionListener
{
mm m1;
Ajb(mm m1)
{
this
.m1
=
m1;
}
public
void
actionPerformed(ActionEvent e)
{
m1.stact(e);
}
}
}
//
@jve:decl-index=0:visual-constraint="17,49"
//
文本方式
public
class
Transform
{
private
String integerStr;
private
String decimalStr;
private
StringBuffer moneyStr
=
new
StringBuffer(
""
);
private
int
pointLocation
=
-
1
;
public
static
final
char
[] units
=
{
'
角
'
,
'
圆
'
,
'
万
'
,
'
亿
'
}
;
public
static
final
char
[] num
=
{
'
零
'
,
'
壹
'
,
'
贰
'
,
'
叁
'
,
'
肆
'
,
'
伍
'
,
'
陆
'
,
'
柒
'
,
'
捌
'
,
'
玖
'
}
;
public
Transform(String numberStr)
{
numberStr
=
trim(numberStr);
if
(
!
check(numberStr) )
{
numberStr
=
"
0
"
;
decimalStr
=
"
0
"
;
}
pointLocation
=
hasRadix(numberStr);
cut(numberStr);
}
public
Transform()
{
integerStr
=
"
0
"
;
decimalStr
=
"
0
"
;
}
public
String trim(String str)
{
return
str.replaceAll(
"
^0*
"
,
""
);
}
public
boolean
check(String str)
{
if
(
!
str.matches(
"
\\d+.?\\d*
"
) )
{
return
false
;
}
else
{
return
true
;
}
}
public
int
hasRadix(String number)
{
return
number.indexOf(
'
.
'
);
}
private
void
cut(String numberStr)
{
if
( pointLocation
==
-
1
)
{
if
( numberStr.length()
>
12
)
{
integerStr
=
numberStr.substring(numberStr.length()
-
12
);
}
else
{
integerStr
=
numberStr;
}
decimalStr
=
"
0
"
;
return
;
}
integerStr
=
numberStr.substring(
0
, pointLocation);
integerStr
=
(integerStr.length()
>
12
)
?
integerStr.substring(integerStr.length()
-
12
) : integerStr;
String temp
=
numberStr.substring(pointLocation
+
1
);
if
( hasRadix(temp)
!=
-
1
)
{
temp
=
temp.substring(
0
, hasRadix(temp));
}
decimalStr
=
( temp.length()
>
1
?
temp.substring(
0
,
1
) : temp );
}
public
String toString()
{
char
[][] temp
=
split();
for
(
int
a
=
0
; a
<
temp.length; a
++
)
{
moneyStr.append(toTurnString(temp[a], temp.length
-
a));
}
if
(
!
decimalStr.equals(
"
0
"
) )
{
moneyStr.append(toTurnString(decimalStr.substring(
0
).toCharArray(),
0
));
}
return
moneyStr.toString().equals(
"
圆
"
)
?
"
零圆
"
: moneyStr.toString();
}
private
String toTurnString(
char
[] number,
int
i)
{
StringBuffer temp
=
new
StringBuffer(
""
);
boolean
isZero
=
false
;
int
zeroLen
=
0
;
for
(
int
a
=
0
; a
<
number.length; a
++
)
{
if
( number[a]
==
'
0
'
)
{
zeroLen
++
;
isZero
=
true
;
}
else
{
isZero
=
false
;
}
if
(
!
isZero )
{
if
( zeroLen
!=
0
)
{
temp.append(num[
0
]);
zeroLen
=
0
;
}
temp.append(toTurnNum(number[a])).append(toTurnUnit(number.length
-
a, i));
}
else
{
if
( a
==
number.length
-
1
)
{
temp.append(toTurnUnit(number.length
-
a, i));
}
}
}
return
temp.toString();
}
private
char
toTurnUnit(
int
len ,
int
unit)
{
switch
( len )
{
case
1
:
return
units[unit];
case
2
:
return
'
拾
'
;
case
3
:
return
'
佰
'
;
case
4
:
return
'
仟
'
;
}
return
'
0
'
;
}
private
char
[][] split()
{
int
lenTmp
=
integerStr.length();
int
strArrayLength
=
lenTmp
/
4
+
((lenTmp
-
lenTmp
/
4
*
4
)
>
0
?
1
:
0
);
char
[][] temp
=
new
char
[strArrayLength][];
for
(
int
a
=
temp.length
-
1
; a
>=
0
; a
--
)
{
if
( a
==
0
)
{
temp[a]
=
integerStr.substring(
0
, lenTmp).toCharArray();
return
temp;
}
temp[a]
=
integerStr.substring(lenTmp
-
4
,lenTmp).toCharArray();
lenTmp
-=
4
;
}
return
temp;
}
public
char
toTurnNum(
char
c)
{
return
num[c
-
(
int
)
'
0
'
];
}
}
import
java.io.
*
;
public
class
test
{
public
static
void
main(String[] args)
{
BufferedReader keyboard
=
new
BufferedReader(
new
InputStreamReader(System.in));
String str;
try
{
do
{
str
=
keyboard.readLine();
System.out.println(
new
Transform(str));
}
while
(
!
str.equals(
"
#
"
) );
}
catch
(Exception e)
{
}
}
}
posted on 2007-01-05 17:14
小石头 阅读(692)
评论(0) 编辑 收藏 所属分类:
我的java学习