黑马程序员技术交流社区
标题:
将浮点数转换成人民币读法字符串
[打印本页]
作者:
ppaapc
时间:
2015-10-18 01:16
标题:
将浮点数转换成人民币读法字符串
例如将1006.333转换成壹千零陆元叁角叁分。 如何处理两个及以上连零的情况
作者:
ppaapc
时间:
2015-10-18 01:17
public class test
{
private String[] ch={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
private String[] chcount={"十","百","千"};
private String[] chunit={"亿","万","元"};
private String[] chunit1={"角","分"};
//整数与小数分离并以字符串的形式保存在数组
private String[] divide(double num)
{
long zheng=(long)num;
int ling=(int)((num-zheng)*100);
return new String[] {String.valueOf(zheng),String.valueOf(ling)};
}
//把数字转换成中文
private String toch(String[] numstr)
{
String result = "";
String result1 = "";
String result0 = "";
String a1=numstr[1];//小数
String a2=numstr[0];//整数
int len1 = a1.length();
int len2 = a2.length();
//处理小数
for(int i=0;i<len1;i++)
{
int count1=a1.charAt(i)-48;
String aa=ch[count1]+chunit1[i];
result1 +=aa;
}
//处理整数
for(int i=0;i<=len2-1;i++)
{
int count2=a2.charAt(i)-48;
String bb = "";
switch(len2-1-i)
{
case 11://千亿
bb=ch[count2]+chcount[2];
result0 +=bb;
break;
case 10://百亿
if(count2!=0)
bb=ch[count2]+chcount[1];
else
bb=ch[count2];
result0 +=bb;
break;
case 9://十亿
if(count2!=0)
bb=ch[count2]+chcount[0];
else
bb=ch[count2];
result0 +=bb;
break;
case 8://亿
if(count2!=0)
bb=ch[count2]+chunit[0];
else
bb=ch[count2];
result0 +=bb;
break;
case 7://千万
if(count2!=0)
bb=ch[count2]+chcount[2];
else
bb=ch[count2];
result0 +=bb;
break;
case 6://百万
if(count2!=0)
bb=ch[count2]+chcount[1];
else
bb=ch[count2];
result0 +=bb;
break;
case 5://十万
if(count2!=0)
bb=ch[count2]+chcount[0];
else
bb=ch[count2];
result0 +=bb;
break;
case 4://万
if(count2!=0)
bb=ch[count2]+chunit[1];
else
bb=chunit[1];
result0 +=bb;
break;
case 3://千
if(count2!=0)
bb=ch[count2]+chcount[2];
else
bb=ch[count2];
result0 +=bb;
break;
case 2://百
if(count2!=0)
bb=ch[count2]+chcount[1];
else
bb=ch[count2];
result0 +=bb;
break;
case 1://十
if(count2!=0)
bb=ch[count2]+chcount[0];
else
bb=ch[count2];
result0 +=bb;
break;
case 0://元
if(count2!=0)
bb=ch[count2]+chunit[2];
else
bb=chunit[2];
result0 +=bb;
break;
}
}
result = result0+result1;
return result;
}
public static void main(String[] args)
{
test data=new test();
String[] xx=data.divide(301301030.89111);//直接赋值
System.out.println(data.toch(xx));
}
}
作者:
ppaapc
时间:
2015-10-18 01:19
楼上是我的代码 望解答
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2