求程序一(进来帮个忙吧!)
问题:输入数字,程序处理后自动解析为人民币字样!
(例如:输入1234.43; 输出为“壹仟贰佰叁拾肆圆肆角叁分)
最小精确到小数点后两位,最大为一亿!
方法越多越好,先谢谢各位了!
------解决方案--------------------这样的问题还敢拿出来问啊,不会网上一大堆。搜一下,写了这么多
------解决方案--------------------同意,网上有好多
------解决方案--------------------穷举法肯定用的人少:把所有可能的值都列出来,弄个映射表,key就是数字,value就是对应的字
------解决方案--------------------以前看到的一个,希望对你有帮助:
import java.io.*;
//将浮点数转化为中文写法的类
class ChineseMoney{
private String number[]={ " ", "壹 ", "贰 ", "叁 ", "肆 ", "伍 ", "陆 ", "柒 ", "捌 ", "玖 "};
private String unit[]={ "元 ", "拾 ", "佰 ", "仟 ", "万 ", "拾 ", "佰 ", "仟 ", "亿 ", "拾 ", "佰 "};
private String small[]={ "角 ", "分 "};
private String strNumber,strUnit,strAll;
private String onlyInt(int intInt)
{
String strInt;
strInt=String.valueOf(intInt);
strNumber= " ";
strUnit= " ";
strAll= " ";
int l=strInt.length ();
int j,k,zeorCount;
zeorCount=0;
for (k=0;k <l;k++)
{
String strTemp=strInt.substring(k,k+1);
int intTemp=Integer.parseInt(strTemp);
strNumber=number[intTemp];
j=l-1-k;
strUnit=unit[j];
if (intTemp==0)
{
if (zeorCount==0)
{
strUnit=strUnit.replace( '拾 ', '零 ');
strUnit=strUnit.replace( '佰 ', '零 ');
strUnit=strUnit.replace( '仟 ', '零 ');
strUnit=strUnit.replace( '万 ', '零 ');
}
else
{
strUnit=strUnit.replaceAll( "拾 ", " ");
strUnit=strUnit.replaceAll( "佰 ", " ");
strUnit=strUnit.replaceAll( "仟 ", " ");
strUnit=strUnit.replaceAll( "万 ", " ");
}
zeorCount++;
}
strAll+=strNumber+strUnit;
}
return strAll;
}
private String onlySmall(int intSmall)
{
strNumber= " ";strUnit= " ";strAll= " ";
String strSmall,strTemp;
strSmall=String.valueOf(intSmall);
int i;
if (intSmall> =10)
{
for (i=0;i <strSmall.length();i++)
{
strTemp=String.valueOf(intSmall).substring(i,i+1);
if (Integer.parseInt(strTemp)!=0)
{
strNumber=number[Integer.parseInt(strTemp)];
strUnit=small[i];
strAll+=strNumber+strUnit;
}
}
}
else
{
if (intSmall!=0)
{
strNumber=number[intSmall];
strUnit=small[1];
strAll+=strNumber+strUnit;
}
}
return strAll;