浮点数转换为人名币读法字符串(JAVA)

/*《java疯狂讲义》浮点数转换为人名币读法字符串这个用例,感觉没有考虑零的情况*/

import java.util.Arrays;

public class Num2Rmb {

private String[] hanArr={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
private String[] unitArr={"","拾","佰","仟","万","亿","角","分"};

public String[] divide(double num){
long zheng=(long)num;
long xiao=Math.round((num-zheng)*100);
String[] str=new String[]{zheng+"",String.valueOf(xiao)};
return str;
}
public String toHanString(String[] numStr){
String zhengStr="";
char[] zhengChr=numStr[0].toCharArray();
int[] zhengInt=new int[zhengChr.length];
for(int i=zhengChr.length;i>0;i--){
zhengInt[i-1]=zhengChr[i-1]-'0';
if(zhengInt[i-1]==0){
if((zhengChr.length-i)%4!=0){
zhengStr=hanArr[0]+zhengStr;
}
while(i>0&&zhengInt[i-1]==0){
i--;
zhengInt[i-1]=zhengChr[i-1]-'0';
}

}
if((zhengChr.length-i)==4)
zhengStr=unitArr[4]+zhengStr;
else if((zhengChr.length-i)==8)
zhengStr=unitArr[5]+zhengStr;
zhengStr=hanArr[zhengInt[i-1]]+unitArr[(zhengChr.length-i)%4]+zhengStr;
}
String xiaoStr="";
char[] xiaoChr=numStr[1].toCharArray();
int[] xiaoInt=new int[xiaoChr.length];
for(int j=0;j<xiaoInt.length;j++){
xiaoInt[j]=xiaoChr[j]-'0';
xiaoStr=xiaoStr+hanArr[xiaoInt[j]]+unitArr[j+6];

}
return zhengStr+xiaoStr;
}
public static void main(String[] args) {
Num2Rmb nr=new Num2Rmb();
double num=6010.333;
System.out.println(Arrays.toString(nr.divide(num)));
System.out.println(nr.toHanString(nr.divide(num)));

}

}

上一篇:Java面试题-多线程


下一篇:Java泛型解析(03):虚拟机运行泛型代码