java 数字转汉字

package xhkj.yhlong;

import java.math.BigDecimal;

public class TestMoney {

private static final String[] numberArr = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };

public static String translate(String numMoney) {
if(numMoney==null){
return numMoney;
}
if("".equals(numMoney)){
return numMoney;
}

String chinMoney="";
int length = numMoney.length();
if(length<2){
chinMoney = numberToChinese(numMoney);
}else {
int count = 0;
for(int i=0;i<length;i++){
String tmpNum = numberToChinese(String.valueOf(numMoney.charAt(length-1-i)));
chinMoney = chinMoney+getUnit(count)+tmpNum;
count++;
}
StringBuilder sb = new StringBuilder(chinMoney);
sb.reverse();

while((sb.indexOf("零亿")!=-1)||(sb.indexOf("零万")!=-1)||
(sb.indexOf("零千")!=-1)||(sb.indexOf("零百")!=-1)||
(sb.indexOf("零十") != -1)||(sb.indexOf("亿万") != -1)||
(sb.indexOf("零零") != -1)){

if(sb.indexOf("零亿")!=-1){
sb.replace(sb.indexOf("零亿"),sb.indexOf("零亿")+2, "亿");
}
if(sb.indexOf("零万")!=-1){
sb.replace(sb.indexOf("零万"),sb.indexOf("零万")+2, "万");
}
if(sb.indexOf("零千")!=-1){
sb.replace(sb.indexOf("零千"),sb.indexOf("零千")+2, "零");
}
if(sb.indexOf("零百")!=-1){
sb.replace(sb.indexOf("零百"),sb.indexOf("零百")+2, "零");
}
if(sb.indexOf("零十") != -1){
sb.replace(sb.indexOf("零十"),sb.indexOf("零十")+2, "零");
}

if(sb.indexOf("亿万") != -1){
sb.replace(sb.indexOf("亿万"),sb.indexOf("亿万")+2, "亿");
}
if(sb.indexOf("零零") != -1){
sb.replace(sb.indexOf("零零"),sb.indexOf("零零")+2, "零");
}
}
chinMoney = sb.toString();
if(chinMoney.endsWith("零")){
chinMoney = chinMoney.substring(0,chinMoney.length()-1);
}
System.out.println(chinMoney);
}


return chinMoney;
}

/**
* msg 获取相应的位数的单位量词
* @return
*/
public static String getUnit(int count){
String unit = "";

switch(count){
case 0:
unit="";
break;
case 1:
case 5:
case 9:
unit="十";
break;
case 2:
case 6:
case 10:
unit="百";
break;
case 3:
case 7:
case 11:
unit="千";
break;
case 4:
unit="万";
break;
case 8:
unit="亿";
break;

}

return unit;
}

/**
* @msg 数字转汉字
* @return
*/
public static String numberToChinese(String number){
if(number==null){
return number;
}
if("".equals(number)){
return number;
}

return numberArr[Integer.parseInt(number)];
}

public static void main(String[] args) throws Exception {
System.out.println(translate("110011001"));
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值