金额转换,阿拉伯数字的金额转换成中国传统的形式如:
(¥1011)->(一千零一拾一元整)输出。
出现连续两个0时没做控制。
package star;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
read();
}
public static void read(){
Scanner scanner = new Scanner(System.in);
String num = scanner.next().trim();
int len = num.length();
StringBuffer sb = new StringBuffer();
if(len>=9){
int len_yi = len - 8;
while(len_yi!=0){
String num_yi = num.substring(0,len_yi);
sb.append(translate(num_yi.substring(0, 1))).append(translateToP(len_yi));
num = num.substring(1);
len_yi--;
}
sb.append(translateToP(len_yi));
len = 8;
}
while(len!=0){
String num_wan = num;
sb.append(translate(num_wan.substring(0, 1))).append(translateToP(len));
num = num.substring(1);
len--;
}
System.out.println(sb.toString());
}
public static String translateToP(int p){
String result;
switch (p){
case 0:
result = "亿";
break;
case 1:
result = "";
break;
case 2:
result = "十";
break;
case 3:
result = "百";
break;
case 4:
result = "千";
break;
case 5:
result = "万";
break;
case 6:
result = "十";
break;
case 7:
result = "百";
break;
case 8:
result = "千";
break;
case 9:
result = "亿";
break;
default:
result = "整";
}
return result;
}
public static String translate(String num){
String result;
int n = Integer.valueOf(num);
switch (n){
case 0:
result = "零";
break;
case 1:
result = "一";
break;
case 2:
result = "二";
break;
case 3:
result = "三";
break;
case 4:
result = "四";
break;
case 5:
result = "五";
break;
case 6:
result = "六";
break;
case 7:
result = "七";
break;
case 8:
result = "八";
break;
case 9:
result = "九";
break;
default:
result = "零";
}
return result;
}
}