您的位置:首页 > 其它

金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)-(一千零一拾一元整)输出。

2016-11-08 00:00 399 查看
public class Money {

private static final char[] data = new char[] { '零', '壹', '贰', '叁', '肆',

'伍', '陆', '柒', '捌', '玖' };

private static final char[] units = new char[] { '元', '拾', '佰', '仟', '万',

'拾', '佰', '仟', '亿' };

public static String convert(int money) {

StringBuffer sbf = new StringBuffer();

int unit = 0;

while (money != 0) {

sbf.insert(0, units[unit++]);

int number = money % 10;

sbf.insert(0, data[number]);

money /= 10;

}

//未去零方法 如:100000000结果:壹亿零仟零佰零拾零万零仟零佰零拾零元
//

return sbf.toString();

//去零方法 如:100000000结果:壹亿元

return sbf.toString().replaceAll("零[拾佰仟万亿]","零").replaceAll(

"零+万","万").replaceAll("零+元","元").replaceAll("零+","零");

}

/**

* Test

* @param args

*/

public static void main(String[] args) {

System.out.println(convert(100000000));

}
}


import
java.math.*;

public
class
TestTrans {

private
static
final
String NUM=
"零壹贰叁肆伍陆柒捌玖"
;

private
static
final
String UNIT=
"亿仟佰拾万仟佰拾元角分厘毫"
;
//13位

private
static
final
String EX_UNIT=
"亿拾佰仟万"
;


//将数值串转换成大写金额:一个循环就行了

//若数值串非法,则返回:null

public
static
String toCapNumber(String data)

{

String s=checkValue(data);

if
(s==
null
){
return
null
;}
//数值串非法

if
(s.charAt(
0
)==
'0'
){
return
"零元整"
;}

String rs=
""
;
//最终结果串

boolean
hasZero=
false
;
//有零没有输出,则置为true

int
i=
0
,sLen=s.length(),uLen=UNIT.length();

int
pos=uLen-sLen+i;
//i所对应的单位串中的位置

for
(;i<sLen;i++,pos++)
//一个循环,即求出所有的大写金额串

{

if
(s.charAt(i)==
'0'
)
//若是字符'0'

{

hasZero=
true
;

if
(isUnit(i,sLen))
//若对应单位是'亿'、万、元,则输出。

{

String u=getUnit(i, sLen);

if
(u.endsWith(
"亿"
)&&!rs.endsWith(
"亿"
)||

   
u.endsWith(
"万"
)&&!rs.endsWith(
"亿"
)||

   
u.endsWith(
"元"
))

{

 
rs=rs+getUnit(i, sLen);
//将单位输出

 
hasZero=
false
;

}

}

}

else

{
//不是'0'

if
(hasZero){rs=rs+
"零"
;hasZero=
false
;}

rs=rs+NUM.charAt(s.charAt(i)-
'0'
)+getUnit(i, sLen);

}

}
//for(i) 大写金额转换结束


if
(
"分厘毫"
.indexOf(rs.charAt(rs.length()-
1
))==-
1
)
//补整

{

rs=rs+
"整"
;

}

return
rs;

}
//大写金额转换结束


//检查数值串。不正确返回null。否则返回整数值串(小数点向右移4位)

private
static
String checkValue(String data)

{

BigDecimal bd=
null
;

BigInteger bi=
null
;

try
{

 
bd=
new
BigDecimal(data,MathContext.DECIMAL128).movePointRight(
4
);

}
catch
(NumberFormatException e){
return
null
;}

bi=(bd.add(
new
BigDecimal(
0.5
))).toBigInteger();

return
bi.toString();

}

//当前位置对应的单位若是亿、万、元,则返回true

private
static
boolean
isUnit(
int
pos,
int
len)

{

String u=getUnit(pos, len);

return
u.endsWith(
"亿"
)||u.endsWith(
"万"
)||u.endsWith(
"元"
);

}

//返回当前位置对应的单位

private
static
String getUnit(
int
pos,
int
len)

{

int
upos=UNIT.length()-len+pos;

if
(upos<
0
)

{

upos=-upos;

if
(upos%
5
==
0
){
return
getStr(upos/
5
);}

return
""
+EX_UNIT.charAt(upos%
5
);

}

return
""
+UNIT.charAt(upos);

}

//返回:亿、亿亿、亿亿亿、亿亿亿亿、。。。。。

private
static
String getStr(
int
i)

{

StringBuilder sb=
new
StringBuilder(
"亿"
);

for
(
int
j=
1
;j<=i;j++)

{

sb.append(
'亿'
);

}

return
sb.toString();

}


public
static
void
main(String[] args) {

System.out.println(
"0.000=>"
+toCapNumber(
"0.000"
));

System.out.println(
"0.50000=>"
+toCapNumber(
"0.50000"
));

System.out.println(
"0.056478154e3=>"
+toCapNumber(
"0.056478154e3"
));

System.out.println(
"2000300004000.5600=>"
+toCapNumber(
"2000300004000.5600"
));

System.out.println(
"30000000002000000000.234=>"
+toCapNumber(
"30000000002000000000.234"
));

}


}

程序运行结果:
0.000=>零元整
0.50000=>伍角整
0.056478154e3=>伍拾陆元肆角柒分捌厘贰毫
2000300004000.5600=>贰万零叁亿零肆仟元伍角陆分
30000000002000000000.234=>叁拾亿亿亿零贰拾亿元贰角叁分肆厘
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐