您的位置:首页 > 其它

阿拉伯数字的金额转换成中国传统的形式

2014-07-27 21:39 197 查看
金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)->(一千零一拾一元整)输出。

代码如下:

private static void asw03() {
// TODO Auto-generated method stub

System.out.println("请输入金额");
Scanner scanner = new Scanner(System.in);
String string = scanner.next(), result = "";
char c;
string = string.substring(1);
Map<Character, String> a = new HashMap<Character, String>();
a.put('0', "零");
a.put('1', "壹");
a.put('2', "贰");
a.put('3', "叁");
a.put('4', "肆");
a.put('5', "伍");
a.put('6', "陆");
a.put('7', "柒");
a.put('8', "捌");
a.put('9', "玖");
for (int i = 0; i < string.length(); i++) {
c = string.charAt(i);
// 当输入c正好在“万”“亿”这两个等级时候且c为0,仅输出此时的单位,例子:120 0001
if (c == '0' &&(i != string.length()-1)&& (string.charAt(i + 1) == '0')
&& (string.length() - i - 1) % 4 == 0) {
result += danwei(string.length() - i - 1);
}
// 当c为零且后一位也为0时候,本次的零不输入,这样保证多个零时只有一个零被输出
else if ((i == string.length() - 1 && c == '0') || c == '0'
&& (string.charAt(i + 1) == '0')) {
continue;
}

// 当输入c正好在“万”“亿”这两个等级时候输出此时的单位,例子:8001000
else if (c == '0' && (string.length() - i - 1) % 4 == 0) {
result += danwei(string.length() - i - 1) + a.get(c);
}
// 当输入c不在那两个单位时候不输出单位,例子:201
else if (c == '0') {
result += a.get(c);
} else {
result += a.get(c) + danwei(string.length() - i - 1);
}

}
System.out.println(result);
}

private static String danwei(int a) {
// TODO Auto-generated method stub

switch (a % 4) {
case 0: {
switch (a) {
case 4:
return "万";
case 8:
return "亿";
case 12:
return "亿万";
case 16:
return "兆";
default:
return "元整";

}
}

case 1:
return "拾";
case 2:
return "百";
case 3:
return "千";
default:
break;
}
return "";
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐