最近在作东西的时候,需要使用中文数字与阿拉伯数字进行互相转换.在网搜了一下效果并不是十分理想.没有办法只好自己动手.
现在拿出来与大家分享,并不断完善.
需要注意的一些地方:
1.目前最大上限是以亿为单位.
2.没有实现中文大写钱数的互换.
3.实用的是int型数字,这样上限大概是以十亿为单位.
4.有些中文的数字习惯没有处理(“4亿”=“4万万”,“一十一”=“十一”,“零”=“0”等等)
Java代码如下
/**
*
*
*
*@author<ahref="yzy0612@163.com">yangzhenyu</a>
*
*@version$Revision$
*
*@since2010-8-26
*/
publicclassNumberUtils {
/*
*基本数字单位;
*/
privatestaticfinalString[]units=
{"千","百","十",""};//个位
/*
*大数字单位;
*/
privatestaticfinalString[]bigUnits=
{"万","亿"};
/*
*中文数字;
*/
privatestaticfinalchar[]numChars=
{'一','二','三','四','五','六','七','八','九'};
//private static final char[] numMouneyChars =
//{ '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' };
privatestaticcharnumZero='零';
/**
*将中文数字转换为阿拉伯数字;
*
*@paramnumberCN
*@return
*/
publicstaticintnumberCN2Arab(String numberCN) {
String tempNumberCN = numberCN;
//异常数据处理;
if(tempNumberCN ==null) {
return0;
}
/*
*nums[0]保存以千单位;nums[1]保存以万单位;nums[2]保存以亿单位;
*/
String[] nums =newString[bigUnits.length+ 1];
//千位以内,直接处理;
nums[0] = tempNumberCN;
/*
*分割大数字,以千为单位进行运算;
*/
for(inti = (bigUnits.length- 1); i >= 0; i--) {
//是否存在大单位(万,亿...);
intfind = tempNumberCN.indexOf(bigUnits[i]);
if(find != -1) {
String[] tempStrs = tempNumberCN.split(bigUnits[i]);
//清空千位内容;
if(nums[0] !=null) {
nums[0] =null;
}
if(tempStrs[0] !=null) {
nums[i + 1] = tempStrs[0];
}
if(tempStrs.length> 1) {
tempNumberCN = tempStrs[1];
if(i == 0) {
nums[0] = tempStrs[1];
}
}else{
tempNumberCN =null;
break;
}
}
}
String tempResultNum ="";
for(inti = nums.length- 1; i >= 0; i--) {
if(nums[i] !=null) {
tempResultNum +=numberKCN2Arab(nums[i]);
}else{
tempResultNum +="0000";
}
}
returnInteger.parseInt(tempResultNum);
}
/**
*将一位中文数字转换为一位数字;eg:一返回1;
*
*@paramonlyCNNumber
*@return
*/
publicstaticintnumberCharCN2Arab(charonlyCNNumber) {
if(numChars[0] == onlyCNNumber) {
return1;
}elseif(numChars[1] == onlyCNNumber || onlyCNNumber =='两') {//处理中文习惯用法(二,两)
return2;
}elseif(numChars[2] == onlyCNNumber) {
return3;
}elseif(numChars[3] == onlyCNNumber) {
return4;
}elseif(numChars[4] == onlyCNNumber) {
return5;
}elseif(numChars[5] == onlyCNNumber) {
return6;
}elseif(numChars[6] == onlyCNNumber) {
return7;
}elseif(numChars[7] == onlyCNNumber) {
return8;
}elseif(numChars[8] == onlyCNNumber) {
return9;
}
return0;
}
/**
*将一位数字转换为一位中文数字;eg:1返回一;
*
*@paramonlyArabNumber
*@return
*/
publicstaticcharnumberCharArab2CN(charonlyArabNumber) {
if(onlyArabNumber =='0') {
returnnumZero;
}
if(onlyArabNumber >'0'&& onlyArabNumber <='9') {
returnnumChars[onlyArabNumber -'0'- 1];
}
returnonlyArabNumber;
}
/**
*
*@paramnum
*@return
*/
publicstaticString numberArab2CN(Integer num) {
String tempNum = num +"";
//传说中的分页算法;
intnumLen = tempNum.length();
intstart = 0;
intend = 0;
intper = 4;
inttotal = (int) ((numLen + per - 1) / per);
intinc = numLen % per;
/*
* 123,1234,1234四位一段,进行处理;
*/
String[] numStrs =newString[total];
for(inti = total - 1; i >= 0; i--) {
start = (i - 1) * per + inc;
if(start < 0) {
start = 0;
}
end = i * per + inc;
numStrs[i] = tempNum.substring(start, end);
}
String tempResultNum ="";
intrempNumsLen = numStrs.length;
for(inti = 0; i < rempNumsLen; i++) {
//小于1000补零处理;
if(i > 0 && Integer.parseInt(numStrs[i]) < 1000) {
tempResultNum +=numZero
+numberKArab2CN(Integer.parseInt(numStrs[i]));
}else{
tempResultNum +=numberKArab2CN(Integer.parseInt(numStrs[i]));
}
//加上单位(万,亿....)
if(i < rempNumsLen - 1) {
tempResultNum +=bigUnits[rempNumsLen - i - 2];
}
}
//去掉未位的零
tempResultNum = tempResultNum.replaceAll(numZero+"$","");
returntempResultNum;
}
/**
*将千以内的数字转换为中文数字;
*
*@paramnum
*@return
*/
privatestaticString numberKArab2CN(Integer num) {
char[] numChars = (num +"").toCharArray();
String tempStr ="";
intinc =units.length- numChars.length;
for(inti = 0; i < numChars.length; i++) {
if(numChars[i] !='0') {
tempStr +=numberCharArab2CN(numChars[i]) +units[i + inc];
}else{
tempStr +=numberCharArab2CN(numChars[i]);
}
}
//将连续的零保留一个
tempStr = tempStr.replaceAll(numZero+"+",numZero+"");
//去掉未位的零
tempStr = tempStr.replaceAll(numZero+"$","");
returntempStr;
}
/**
*处理千以内中文数字,返回4位数字字符串,位数不够以"0"补齐;
*
*@paramnumberCN
*@return
*/
privatestaticString numberKCN2Arab(String numberCN) {
if("".equals(numberCN)) {
return"";
}
int[] nums =newint[4];
if(numberCN !=null) {
for(inti = 0; i <units.length; i++) {
intidx = numberCN.indexOf(units[i]);
if(idx > 0) {
chartempNumChar = numberCN.charAt(idx - 1);
inttempNumInt =numberCharCN2Arab(tempNumChar);
nums[i] = tempNumInt;
}
}
//处理十位
charones = numberCN.charAt(numberCN.length() - 1);
nums[nums.length- 1] =numberCharCN2Arab(ones);
//处理个位
if((numberCN.length() == 2 || numberCN.length() == 1)
&& numberCN.charAt(0) =='十') {
nums[nums.length- 2] = 1;
}
}
//返回结果
String tempNum ="";
for(inti = 0; i < nums.length; i++) {
tempNum += nums[i];
}
return(tempNum);
}
}