黑马程序员技术交流社区
标题:
金额转换,阿拉伯数字转换成中文传统数字
[打印本页]
作者:
fmi110
时间:
2015-9-21 13:26
标题:
金额转换,阿拉伯数字转换成中文传统数字
金额转换,阿拉伯数字转换成中文传统数字
作者:
zjingwen
时间:
2015-9-21 13:26
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
long num = 101000001010l;
String[] digit = {"零","壹","貳","叁","肆","伍","陆","柒","扒","玖"};
String[] unit = {"整","圆","拾","百","仟","万","拾","百","仟","亿","拾","百","仟","万"};
while(true){
num = new Scanner(System.in).nextLong();
char[] chArr = Long.toString(num).toCharArray();
StringBuilder sb = new StringBuilder("");
for(int i = 0;i < chArr.length;i++){
String s = digit[Integer.parseInt(String.valueOf(chArr[i]))];
String s2 = unit[chArr.length-i];
sb.append(s).append(s2);
}
sb.append(unit[0]);
String str = sb.toString();
str = change(str);
System.out.println(str);}
}
private static String change(String str) {
String s = str.replaceAll("零[仟百拾]", "零");
s = s.replaceAll("零+", "零").replaceAll("零亿", "亿").replaceAll("零万", "万");
s = s.replaceAll("零圆", "圆").replace("亿万", "亿");
return s;
}
}
作者:
年强
时间:
2015-9-21 19:43
这种方法叫查表法。。。很有用
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2