【Java】将阿拉伯数字金额转化为中文大写金额表示

今天老师即兴布置了一个作业,将阿拉伯数字金额转化为中文大写金额表示,假设数字上限为小于10亿,可以支持2位小数,如:

  • 100000001——壹亿零壹元整
  • 100100011——壹亿零壹拾万零壹拾壹元整
  • 201000211——贰亿零壹佰万零贰佰壹拾壹元整
  • 2422000.21——贰佰肆拾贰万贰千元贰角壹分

拿到题目,想到先拿两个数组来存放数字和单位,把阿拉伯数字都转换为大写数字和单位组合的形式,如:

  • 112000012——壹亿壹仟贰佰零拾零万零仟零佰壹拾贰元整

然后将转换后的字符串进行加工,将"零[拾佰仟]"等变成"零",考虑"零万"变成"零"还是"万",这一步是比较麻烦的,要考虑比较多的情况,如:

  • 112000012——壹亿壹仟贰佰零零万零零壹拾贰元整

然后将相邻的"零"去重,如:

  • 112000012——壹亿壹仟贰佰万零壹拾贰元整

以下为具体代码,可能还有一些情况没有考虑到,暂时记录如下。

package com.zzutest;

import java.text.DecimalFormat;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Transfer2 {
    public static void main(String[] args) {
    	Scanner scan= new Scanner(System.in);
    	while(true) {
    		System.out.println("请输入您想要转化的数字:");
    		String number=scan.next();
    		if(number.equals("0")) {
    			break;
    		};
            //System.out.println(cov(convert(number)));
        	System.out.println("转换结果为:"+convert(number));
    	}
    }

    private static final char[] data = new char[]{'零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'};
    private static final char[] units = new char[]{'分', '角', '元', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿'};

    private static String removeCharAt(String str, int i) {
		return str.substring(0, i)+str.substring(i+1);
	}
    
//    //将相邻的‘零’去重
//    private static String changeZero(String str) {
//    	str.replace('零', ' ');
//		String no = str.replaceAll("\\s{1,}", " ");
//		no.replace(' ', '零');
//		System.out.println(no);
//    	return no;
//    }
    
    /**
     * 小数类型
     * @param str
     * @return
     */
    private static String cov(String str,String trans) {
    	StringBuffer sb = new StringBuffer(trans);
    	
    	if(Double.parseDouble(str)>100000000) {
    		String wan=str.substring(1, 5);
    		//System.out.println("$$$$$$$$$$$$$$wande    is     "+wan);
    		int wanValue = Integer.parseInt(wan);
    		if(wanValue>0) {
    			trans = sb.toString()
    					.replaceAll("零拾", "零")
    					.replaceAll("零佰", "零")
    					.replaceAll("零仟", "零")
    					.replaceAll("零万", "万")
    					.replaceAll("零元", "元")
    					.replaceAll("零角", "零")
    					.replaceAll("零分", "")
    					.replaceAll("零零", "零")
    					.replaceAll("零零", "零")
    					.replaceAll("零零", "零")
    					.replaceAll("零零", "零")
    					.replaceAll("零零", "零")
    					.replaceAll("零零", "零")
    					.replaceAll("零万", "万");
    			
    		}else if(wanValue==0){
    			trans = sb.toString()
    					.replaceAll("零拾", "零")
    					.replaceAll("零佰", "零")
    					.replaceAll("零仟", "零")
    					.replaceAll("零万", "零")
    					.replaceAll("零元", "元")
    					.replaceAll("零角", "零")
    					.replaceAll("零分", "")
    					.replaceAll("零零", "零")
    					.replaceAll("零零", "零")
    					.replaceAll("零零", "零")
    					.replaceAll("零零", "零")
    					.replaceAll("零零", "零")
    					.replaceAll("零零", "零");
    		}
    	}else if(Double.parseDouble(str)>10000 && Double.parseDouble(str)<100000000) {
			trans = sb.toString()
					.replaceAll("零拾", "零")
					.replaceAll("零佰", "零")
					.replaceAll("零仟", "零")
					.replaceAll("零万", "万")
					.replaceAll("零元", "元")
					.replaceAll("零角", "零")
					.replaceAll("零分", "")
					.replaceAll("零零", "零")
					.replaceAll("零零", "零")
					.replaceAll("零零", "零")
					.replaceAll("零零", "零")
					.replaceAll("零零", "零")
					.replaceAll("零零", "零");
    	}else if(Double.parseDouble(str)<10000 && Double.parseDouble(str)>0) {
    		trans = sb.toString()
					.replaceAll("零拾", "零")
					.replaceAll("零佰", "零")
					.replaceAll("零仟", "零")
					.replaceAll("零元", "元")
					.replaceAll("零角", "零")
					.replaceAll("零分", "")
					.replaceAll("零零", "零")
					.replaceAll("零零", "零")
					.replaceAll("零零", "零")
					.replaceAll("零零", "零")
					.replaceAll("零零", "零")
					.replaceAll("零零", "零");
    	}
    	
        return new StringBuffer(trans).toString();
    }

    /**
     * 整数类型
     * @param str
     * @return
     */
    private static String covZ(String str,String trans) {
    	StringBuffer sb = new StringBuffer(trans);
    	
    	if(Double.parseDouble(str)>100000000) {
    		String wan=str.substring(1, 5);
    		System.out.println("$$$$$$$$$$$$$$wande    is     "+wan);
    		int wanValue = Integer.parseInt(wan);
    		if(wanValue>0) {
    			trans = sb.toString()
    					.replaceAll("零拾", "零")
    					.replaceAll("零佰", "零")
    					.replaceAll("零仟", "零")
    					.replaceAll("零万", "万")
    					.replaceAll("零元", "元")
    					.replaceAll("元", "元整")
    					.replaceAll("零零", "零")
    					.replaceAll("零零", "零")
    					.replaceAll("零零", "零")
    					.replaceAll("零零", "零")
    					.replaceAll("零零", "零")
    					.replaceAll("零零", "零")
    					.replaceAll("零万", "万");
    		}else if(wanValue==0){
    			trans = sb.toString()
    					.replace("零拾", "零")
    					.replace("零佰", "零")
    					.replace("零仟", "零")
    					.replace("零万", "零")
    					.replace("零元", "元")
    					.replace("元", "元整")
    					.replaceAll("零零", "零")
    					.replaceAll("零零", "零")
    					.replaceAll("零零", "零")
    					.replaceAll("零零", "零")
    					.replaceAll("零零", "零")
    					.replaceAll("零零", "零");
    			//System.out.println("_________爆炸了");
    		}
    	}else if(Double.parseDouble(str)>10000 && Double.parseDouble(str)<100000000) {
			trans = sb.toString()
					.replaceAll("零拾", "零")
					.replaceAll("零佰", "零")
					.replaceAll("零仟", "零")
					.replaceAll("零万", "万")
					.replaceAll("零元", "元")
					.replaceAll("元", "元整")
					.replaceAll("零零", "零")
					.replaceAll("零零", "零")
					.replaceAll("零零", "零")
					.replaceAll("零零", "零")
					.replaceAll("零零", "零")
					.replaceAll("零零", "零");
    	}else if(Double.parseDouble(str)<10000 && Double.parseDouble(str)>0) {
    		trans = sb.toString()
					.replaceAll("零拾", "零")
					.replaceAll("零佰", "零")
					.replaceAll("零仟", "零")
					.replaceAll("零元", "元")
					.replaceAll("元", "元整")
					.replaceAll("零零", "零")
					.replaceAll("零零", "零")
					.replaceAll("零零", "零")
					.replaceAll("零零", "零")
					.replaceAll("零零", "零")
					.replaceAll("零零", "零");
    	}
    	
    	
        //StringBuffer sb = new StringBuffer(str);

        //System.out.println("1"+sb.reverse().toString());
        //str = sb.reverse().toString().replaceAll("零[拾佰仟]", "零").replaceAll("零+万", "万").replaceAll("零+元", "元").replaceAll("零+", "零");
       // System.out.println("2"+str);
        return new StringBuffer(trans).toString();
    }
    
    /**
     * 现将各个后面统统加上单位 
     * @param money
     * @return
     */
    public static String convert(String money) {
    	//判断输入是否包含小数点
    	//包含小数点
    	if(money.contains(".")) {
    		//将字符串转为double类型
    		double moneyT=Double.parseDouble(money);
            DecimalFormat df = new DecimalFormat(".00");
//            System.out.println("##############"+df.format(moneyT));
            //得到标准的小数字符串,即小数点后两位数
            String moneyC=df.format(moneyT);
            //小数点的位置
            int location = moneyC.indexOf(".");
            //System.out.println("the location is "+location);
            //除去小数点后
            //System.out.println("the location which has been conved is "+removeCharAt(moneyC,location));
            //使用double类型来标识该数字
            double finalNum=Double.parseDouble(removeCharAt(moneyC,location));
    		//声明定义StringBuffer来存转换后的字符串
    		StringBuffer sbf = new StringBuffer();
	        int unit = 0;
	        //逐位进行大写化,最低位开始依次向上,得到的字符串是倒序的
	        while (finalNum > 1) {
	        	//单位
	            sbf.insert(0, units[unit++]);
	            double number = finalNum % 10;
	            sbf.insert(0, data[(int)number]);
	            finalNum /= 10;
	            //System.out.println("**********"+finalNum);
	        }
	        //System.out.println("加上单位以后未经转换的格式为:  "+sbf.toString());
	        String judged = cov(money,sbf.toString());
	        //System.out.println("经过格式判断的数字为:   " + judged);
	        return judged;
    	}
    	//不包含小数点的输入
    	else {
    		int moneyT=Integer.parseInt(money);
    		//System.out.println("---------------->int类型:"+moneyT);
    		StringBuffer sbf = new StringBuffer();
    	        int unit = 2;
    	        while (moneyT != 0) {
    	            sbf.insert(0, units[unit++]);
    	            int number = moneyT % 10;
    	            sbf.insert(0, data[number]);
    	            moneyT /= 10;
    	        }
    	        //System.out.println("加上单位以后未经转换的格式为:  "+sbf.toString());
    	        String judged = covZ(money,sbf.toString());
    	        //System.out.println("经过格式判断的数字为:   " + judged);
    	        return judged;
    	}
       
    }
}

发布了31 篇原创文章 · 获赞 6 · 访问量 4185

猜你喜欢

转载自blog.csdn.net/zhenliangit0918/article/details/104289239