求一算法,价格数是:549,950.30(根据前面的结果而变化),要转换成以下的表达形式:
Five Hundred Forty Nine Thousand Nine Hundred Fifty Dollars and Thirty Cents,最大位数考虑到billion就行,没想出太好的方法,请指点一下。
Five Hundred Forty Nine Thousand Nine Hundred Fifty Dollars and Thirty Cents,最大位数考虑到billion就行,没想出太好的方法,请指点一下。
String func1(int number)和String decimal(int cents);
这两个函数的算法超简单,不说了。
2,然后将要表达的树通过逗号(,)和小数点(.)分割,放到一个数组里,(比如549,950.30分割后数组里因该有这些元素: { '549',',','950','.','30'}),然后对小数点前每块数字筷调用func1()方法,对小数点后的调用decimal()方法,至于每块数字块后面得跟一个字符串(billion,million,thousand)这个也好说,自己加。ok了。
{
//用到的字符串集合
static String[] Str = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
static String[] Str2 = {"Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
static String[] Str3 = {"Hundred","Thousand","Million","Billion"}; public static void dealwith(int n, int count)
{
if(n != 0)
{
int temp = n % 10; //分解数位
//递归处理
dealwith(n/10, count+1);
if(temp != 0)
{
if(count == 0) System.out.print(Str[temp-1] + " ");
else if(count == 1) System.out.print(Str2[temp-1] + " ");
else if(count == 2) System.out.print(Str[temp-1] + " " + Str3[0] + " ");
}
}
}
public static void ok()
{
String ss = "4,847,326,574.60";
//按逗号','拆成String数组
String[] s = ss.split(",");
int[] arr = new int[s.length+1];
for(int i = 0; i <= s.length-2; ++i)
{
arr[i] = Integer.parseInt(s[i]);
}
//处理点号'.'分隔的数字
int n1 = Integer.parseInt( s[s.length-1].substring(0,s[s.length-1].indexOf('.')) );
int n2 = Integer.parseInt( s[s.length-1].substring(s[s.length-1].indexOf('.')+1 , s[s.length-1].length() ) ); arr[s.length-1] = n1;
arr[s.length] = n2; for(int i=0; i<arr.length; ++i)
{
dealwith(arr[i],0);
if(i <= 2)
System.out.print(Str3[Str3.length - i - 1] + " ");
if( i == arr.length - 2)
System.out.print("Dollars and ");
if( i == arr.length - 1)
System.out.print("Cents.");
}
System.out.println();
}
public static void main(String[] args)
{
ok();
}
}
Five Hundred Forty Nine Billion Nine Hundred Fifty Million Dollars and Thirty Thousand Cents.我还在找原因,如再能指点一二,感激不尽。
{
//用到的字符串集合
static String[] Str = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
static String[] Str2 = {"Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
static String[] Str3 = {"Hundred","Thousand","Million","Billion"}; public static void dealwith(int n, int count)
{
if(n != 0)
{
int temp = n % 10; //分解数位 //递归处理
dealwith(n/10, count+1); if(temp != 0)
{
if(count == 0) System.out.print(Str[temp-1] + " ");
else if(count == 1) System.out.print(Str2[temp-1] + " ");
else if(count == 2) System.out.print(Str[temp-1] + " " + Str3[0] + " ");
}
}
}
public static void ok()
{
String ss = "326,574.60";//按逗号','拆成String数组
String[] s = ss.split(","); int[] arr = new int[s.length+1]; for(int i = 0; i <= s.length-2; ++i)
{
arr[i] = Integer.parseInt(s[i]);
} //处理点号'.'分隔的数字
int n1 = Integer.parseInt( s[s.length-1].substring(0,s[s.length-1].indexOf('.')) ); int n2 = Integer.parseInt( s[s.length-1].substring(s[s.length-1].indexOf('.')+1 , s[s.length-1].length() ) ); //由于多了这两个元素故出了问题,鄙人算法不是太好噢
arr[s.length-1] = n1;
arr[s.length] = n2; for(int i=0; i <arr.length; ++i)
{
dealwith(arr[i],0); //后加的
if(arr.length == 1+2 && i < 1)
System.out.print(Str3[arr.length - 2 - i] + " ");
if(arr.length == 2+2 && i < 2)
System.out.print(Str3[arr.length - 2 - i] + " ");
if(arr.length == 3+2 && i < 3)
System.out.print(Str3[arr.length - 2 - i] + " "); if( i == arr.length - 2)
System.out.print("Dollars and ");
if( i == arr.length - 1)
System.out.print("Cents.");
}
System.out.println();
} public static void main(String[] args)
{
ok();
}
}甭客气!!!!!!!