数字到大写人民币转换

import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner scanner=new Scanner(System.in);
		String str=scanner.next();
		System.out.println(convert(str));
	}
	
	public static String convert(String number){
		StringBuilder sb=new StringBuilder();
		String str;
		String result;
		int index=number.indexOf('.');
		long num;
		if(index!=-1){
		    num=Long.parseLong(number.substring(0,index));
		}else{
			num=Long.parseLong(number);
		}
		int temp;
		int i=0;
		String[] numTables = new String[]{"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
		String[] levelTables = new String[]{"万", "亿"};
		String[] mulTables = new String[]{"", "拾", "佰", "仟"};
		String[] digitTables = new String[]{"角", "分"};
		while(num>0){
			temp=(int) (num%10);
			if(i==4){
				str=numTables[temp]+levelTables[0];
			}else if(i==8){
				str=numTables[temp]+levelTables[1];
				i=0;
			}else{
				str=i<4?numTables[temp]+mulTables[i]:numTables[temp]+mulTables[i-4];
			}
			sb.insert(0, str);
			num=num/10;
			i++;
		}
		
		sb.append("元");
		
		int t=index+1;
		int j=0;
		while(t!=0&&t<number.length()&&j<2){
			temp=number.charAt(t)-'0';
			str=numTables[temp]+digitTables[j];
			sb.append(str);
			t++;
			j++;
		}
		
		
		result=sb.toString();
		result=result.replaceAll("零[仟佰拾角分]", "零").replaceAll("零{2,}", "零").replaceAll("零亿", "亿")
				.replaceAll("零万", "万").replaceAll("亿万", "亿零")
				.replaceAll("零{2,}", "零");
		if(result.endsWith("零")){
			result=result.substring(0,result.length()-1);
		}
		if(result.startsWith("壹拾")){
			result=result.substring(1);
		}
		if(!result.endsWith("角")&&!result.endsWith("分")){
			result+="整";
		}
		return "人民币"+result;
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值