java练习:金额转换,阿拉伯数字转换成中文传统形式

需求:金额转换,阿拉伯数字转换成中文传统形式   ,例如 101000001010   转为     壹仟零壹拾亿零壹仟零壹拾圆整

最终版:

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;
	}
}

有bug版

import java.util.Scanner;


public class Test2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		long num = 101000001010l;//一千零一十亿零一千零一十圆整
		num = 1010l;//一千零一十亿零一千零一十圆整
		while(true){
			
			System.out.println("输入数学数字:");
			num = new Scanner(System.in).nextLong();
			String[] unit = {"圆整","万","亿"};
			int count = -1;
			StringBuilder sb = new StringBuilder("");
			
			while(num>0){
				count++;
				String s = "";
				long n = num%10000;//取后四位
				
				if(n < 10)
					s = "000"+n;
				else if(n < 100 )
					s = "00"+n;
				else if(n < 1000)
					s = "0"+n;    
				else
					s = s+n;
				num /= 10000;
				if(!s.equals("0000"))
					s = toNum(s)+unit[count];//返回的数字加单位  
				else
					s = toNum(s);
				sb.insert(0, s);
//			System.out.println(sb);
			}
			String chineseNum = sb.toString();
			System.out.println(chineseNum);
		}

	}
	public static String toNum(String num){
		String[] digit =	{"零","壹","貳","叁","肆","伍","陆","柒","扒","玖"};
		char[] ch = num.toCharArray();//字符串形式的四位数
		
		int count = 0;//记录字符串含0的个数
		int index = -1;
		while((index = num.indexOf("0",index+1))!= -1)
			count++;
//		System.out.println("字符串含0个数count = "+count);
		String numStr = "";//返回的中文数字
		
		switch(count){
			case 4:
				numStr = "零";
				break;
			case 3:
				int temp = Integer.parseInt(num);
				if(temp<10)
					numStr = digit[ch[3]-'0'];
				else if(temp > 9 && temp < 101)
					numStr = digit[ch[2]-'0']+"拾";
				else if(temp > 99 && temp < 1000)
					numStr = digit[ch[1]-'0'] + "百";
				else
					numStr = digit[ch[0]-'0'] + "仟";
				break;
			case 2:
				if(num.matches("[0][0][1-9][1-9]")){
					numStr = numStr + digit[ch[2]-'0']+"拾"+digit[ch[3]-'0'];;
				}
				else if(num.matches("[1-9][0][0][1-9]")){
					numStr = numStr + digit[ch[0]-'0'] + "仟" +"零"+ digit[ch[3]-'0'];
				}
				else if(num.matches("[1-9][1-9][0][0]")){
					numStr = numStr + digit[ch[0]-'0'] + "仟" +digit[ch[1]-'0'] + "百";
				}
				else if(num.matches("[0][1-9][1-9][0]")){
					numStr = numStr + digit[ch[1]-'0'] + "百"+ digit[ch[2]-'0']+"拾";
				}
				else if(num.matches("[0][1-9][0][1-9]")){
					numStr = numStr + digit[ch[1]-'0'] + "百"+"零"+ digit[ch[3]-'0'];
				}
				else if(num.matches("[1-9][0][1-9][0]")){
					numStr = numStr + digit[ch[0]-'0'] + "仟"+"零"+ digit[ch[2]-'0']+"拾";
				}
				break;
			case 1:
				if(num.matches("[0][1-9][1-9][1-9]")){
					numStr = numStr + digit[ch[1]-'0'] + "百" + digit[ch[2]-'0']+"拾"+digit[ch[3]-'0'];
				}
				else if(num.matches("[1-9][0][1-9][1-9]")){
					numStr = numStr + digit[ch[0]-'0'] + "仟" +"零"+  digit[ch[2]-'0']+"拾"+digit[ch[3]-'0'];
				}
				else if(num.matches("[1-9][1-9][0][1-9]")){
					numStr = numStr + digit[ch[0]-'0'] + "仟" +digit[ch[1]-'0'] + "百" + "零"+digit[ch[3]-'0'];
				}
				else if(num.matches("[1-9][1-9][1-9][0]")){
					numStr = numStr + digit[ch[0]-'0'] + "仟" +digit[ch[1]-'0'] + "百" + digit[ch[2]-'0']+"拾";
				}
				break;
			default:
				numStr = numStr + digit[ch[0]-'0'] + "仟" +digit[ch[1]-'0'] + "百" + digit[ch[2]-'0']+"拾"+digit[ch[3]-'0'];
		}
		
		
//		System.out.println(numStr);
		return numStr;

	}
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值