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

金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥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 "";
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值