求程序一(进来帮个忙吧!)
问题:输入数字,程序处理后自动解析为人民币字样!
(例如:输入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;
}
public String getChineseMoney(double number)
{
//四舍五入
number=(number*100+0.5)/100;
String strAll,strChineseInt,strChineseSmall,strZheng;;
int intInt,intSmall;
strChineseInt= " ";strChineseSmall= " ";strZheng= " ";
//整数部分
intInt=(int)( number*100/100);
if (intInt!=0)
{
strChineseInt=onlyInt(intInt);
}
//小数部分
double temp=(number-intInt)*100*100/100;
//对小数部分四舍五入
intSmall=(int)(temp*100+0.5)/100;
if (intSmall!=0)
{
strChineseSmall=onlySmall(intSmall);
}
else
{
strZheng= "整 ";
}
strAll=strChineseInt+strChineseSmall+strZheng;
return strAll;
}
public static void main(String args[]) throws IOException
{
ChineseMoney cm=new ChineseMoney();
double money;
String strMoney,strChineseMoney;
strMoney= " ";
System.out.println( "输入货币(四舍五入): ");
BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
strMoney = cin.readLine();
money=Double.parseDouble(strMoney);
strChineseMoney=cm.getChineseMoney(money);
System.out.println(strChineseMoney);
}
}
[解决办法]
public class math2 {
public math2(){}
public String setmoney1(int a)
{
switch(a)
{
case 1: return( "圆 ");
case 2:return( "拾 ");
case 3:return( "佰 ");
case 4:return( "仟 ");
case 5:return( "万 ");
case 6:return( "拾万 ");
case 7:return( "佰万 ");
case 8:return( "仟万 ");
case 9:return( "亿 ");
default: return( "错误 ");
}
}
public String setnumber(char b)
{
switch(b)
{
case '1 ': return( "壹 ");
case '2 ':return( "贰 ");
case '3 ':return( "叁 ");
case '4 ':return( "肆 ");
case '5 ':return( "伍 ");
case '6 ':return( "陆 ");
case '7 ':return( "柒 ");
case '8 ':return( "捌 ");
case '9 ':return( "九 ");
default: return( "错误 ");
}
}
public static void main(String [] args)
{
math2 m=new math2();
double n=143453.33;
String a=Double.toString(n);
char [] b=a.toCharArray();
int n_point=a.indexOf( ". ");
int i;
System.out.println(b[0]);
for(i=0;i <n_point-1;i++)
{
System.out.print(m.setnumber(b[i]));
System.out.print(m.setmoney1(n_point-i));
}
System.out.print(m.setnumber(b[n_point+1]));
System.out.print( "角 ");
System.out.print(m.setnumber(b[n_point+2]));
System.out.print( "分 ");
}
}
试写了一个 LZ参考一下