package test;
import java.util.*;
public class num2rmb {
private String[] hanArr= {"零" , "壹" , "贰" , "叁" , "肆" ,
"伍" , "陆" , "柒" , "捌" , "玖"};
private String[] unitArr = {"十", "百", "千"};
private String toHanStr(String numStr)
{
int strLen = numStr.length();
int yu = strLen % 4;
//partNum表示该数字字符串可划分成的节数(每4位为一节)。
//如余数等于0,则将strLen/4赋值给partNum;否则将strLen/4 + 1赋值给partNum。
int partNum = yu == 0 ? strLen/4 : strLen/4 + 1;
//firstPartLen表示第一节的长度
int firstPartLen = strLen - (partNum - 1)*4; //因为后面节的长度是总节数减-1,而后面肯定是满4个的
String resultFirst = "";
String resultSecond = "";
String resultThird = "";
for (int i = 0 ; i < partNum ; i ++ )
{
if (i == 0)
{
String firstPart = numStr.substring(0, firstPartLen);
for(int j=0;j<firstPartLen;j++)
{
int num=firstPart.charAt(j)-48;
if(j!=firstPartLen-1&&num!=0)
{
resultFirst += hanArr[num] + unitArr[firstPartLen - 2 - j];
}
else if (j==firstPartLen-1&&num!=0)
{
resultFirst += hanArr[num];
}
else if(num==0)
{
if(((j-1)>=0)&&firstPart.charAt(j-1)-48==0) continue;
if(((j-1)>=0)&&firstPart.charAt(j-1)-48!=0&&((j+1)<=firstPartLen-1)&&firstPart.charAt(j+1)-48!=0)
resultFirst += hanArr[num];
}
}
}
else if(i==1)
{
String secondPart = numStr.substring(firstPartLen, (firstPartLen + 4));
int secPartLen = 4;
for(int j=0;j<secPartLen;j++)
{
int num=secondPart.charAt(j)-48;
if (Integer.parseInt(secondPart) == 0 && partNum == 3)
{
resultSecond = hanArr[0];
break;
}
else if(j!=secPartLen-1&&num!=0)
{
resultSecond += hanArr[num] + unitArr[secPartLen - 2 - j];
}
else if (j==secPartLen-1&&num!=0)
{
resultSecond += hanArr[num];
}
else if(num==0)
{
if(((j-1)>=0)&&secondPart.charAt(j-1)-48==0) continue;
if(((j-1)>=0)&&secondPart.charAt(j-1)-48!=0&&((j+1)<=secPartLen-1)&&secondPart.charAt(j+1)-48!=0)
resultSecond += hanArr[num];
}
}
}
else if(i==2)
{
String thirdPart = numStr.substring((firstPartLen + 4), strLen);
int thirdPartLen = 4;
for(int j=0;j<thirdPartLen;j++)
{
int num=thirdPart.charAt(j)-48;
if(j!=thirdPartLen-1&&num!=0)
{
resultThird += hanArr[num] + unitArr[thirdPartLen - 2 - j];
}
else if (j==thirdPartLen-1&&num!=0)
{
resultThird += hanArr[num];
}
else if(num==0)
{
if(((j-1)>=0)&&thirdPart.charAt(j-1)-48==0) continue;
if(((j-1)>=0)&&thirdPart.charAt(j-1)-48!=0&&((j+1)<=thirdPartLen-1)&&thirdPart.charAt(j+1)-48!=0)
resultThird += hanArr[num];
}
}
}
}
String result = "";
if (partNum == 1)
{
//如果数字仅为0
if (resultFirst == "")
{
result = hanArr[0] + "元";
}
else
{
result = resultFirst + "元";
}
}
else if (partNum == 2)
{
//如果第二节全为0
if (Integer.parseInt(numStr.substring(firstPartLen, (firstPartLen + 4))) == 0)
{
result = resultFirst + "万";
}
else
{
result = resultFirst + "万" + " " + resultSecond+ "元";
}
}
else if (partNum == 3)
{
if (Integer.parseInt(numStr.substring(firstPartLen, (firstPartLen + 4))) == 0
&& Integer.parseInt(numStr.substring((firstPartLen + 4), strLen)) == 0)
{
result = resultFirst + "亿";
}
else if(Integer.parseInt(numStr.substring(firstPartLen, (firstPartLen + 4))) == 0)
{
result = resultFirst + "亿" + " " + resultSecond + " " + resultThird+ "元";
}
else if(Integer.parseInt(numStr.substring((firstPartLen + 4), strLen)) == 0)
{
result = resultFirst + "亿" + " " + resultSecond + "万" + " " + "元";
}
else
{
result = resultFirst + "亿" + " " + resultSecond + "万" + " " + resultThird+ "元";
}
}
return result;
}
public static void main(String[] args)
{
//等号左边的Num2Rmb是nr的变量类型,右边的Num2Rmb是构造器名
num2rmb nr = new num2rmb();
//测试把一个12位以内的数字字符串变成汉字字符串
System.out.println(nr.toHanStr("12345678900"));
}
}
本人Java新手,这个程序也有待改进,请指教,注释没写,见谅了。。。