用java代码将阿拉伯数字金额转换成中文大写

思路:首先,利用String的特性,将金额分成整数部分和小数部分,进行分开处理,然后再用if条件进行判断,具体代码如下:

public class getChineseMoney(){
    public static void main(String srgs[]){
        System.out.println("转换之前的金额为:1824512.54");
        System.out.print("转换之后的金额为:");
        System.out.print(ChineseMoney(1824512.54)):
    }
    public static String ChineseMoney(String money){
        String text = transChineseMoney1(money) +                     transChineseMoney2(money);
        /*这里的思路是,如果金额没有小数部分,则在后面加上“零分”
        “Pattern.CASE_INSENSITIVE”的意思是不对大小写进行区分*/
        Pattern p = Pattern.compile("零分",Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(text);         
    return text; 
    }
    /* 截取金额的整数部分,并将其转换成中文大写格式 */ 
    public static String transChineseMoney1(String s){  
        String ss = s; 
        String tmpnewchar = ""; 
//以“.”将ss分成两段,part[0]为整数部分,part[1]为小数部分
        String[] part = ss.split("\\.");  
        if (part[0].length() > 10) { // 超出可转换位数
             return"";        
        } 
        for (int i = 0; i < part[0].length(); i++) { 
            char perchar = part[0].charAt(i); 
            if (perchar == '0') 
                tmpnewchar = tmpnewchar + "零"; 
                if (perchar == '1') 
                tmpnewchar = tmpnewchar + "壹"; 
                if (perchar == '2') 
                tmpnewchar = tmpnewchar + "贰"; 
                if (perchar == '3') 
                tmpnewchar = tmpnewchar + "叁"; 
                if (perchar == '4') 
                tmpnewchar = tmpnewchar + "肆"; 
                if (perchar == '5') 
                tmpnewchar = tmpnewchar + "伍"; 
                if (perchar == '6') 
                tmpnewchar = tmpnewchar + "陆"; 
                if (perchar == '7') 
                tmpnewchar = tmpnewchar + "柒"; 
                if (perchar == '8') 
                tmpnewchar = tmpnewchar + "捌"; 
                if (perchar == '9') 
                tmpnewchar = tmpnewchar + "玖";  
                int j = part[0].length() - i - 1; 
                if (j == 0) 
                tmpnewchar = tmpnewchar + "圆"; 
                if (j == 1 && perchar != 0) 
                tmpnewchar = tmpnewchar + "拾"; 
                if (j == 2 && perchar != 0) 
                tmpnewchar = tmpnewchar + "佰"; 
                if (j == 3 && perchar != 0) 
                tmpnewchar = tmpnewchar + "仟"; 
                if (j == 4 && perchar != 0) 
                tmpnewchar = tmpnewchar + "万"; 
                if (j == 5 && perchar != 0) 
                tmpnewchar = tmpnewchar + "拾"; 
                if (j == 6 && perchar != 0) 
                tmpnewchar = tmpnewchar + "佰"; 
                if (j == 7 && perchar != 0) 
                tmpnewchar = tmpnewchar + "仟"; 
                if (j == 8 && perchar != 0) 
                tmpnewchar = tmpnewchar + "亿"; 
                if (j == 9 && perchar != 0) 
                tmpnewchar = tmpnewchar + "拾";       } 
                return tmpnewchar;  
/* 截取金额的小数部分,并将其转换成中文大写格式 */ 
     public static String transChineseMoney2(String s) {  
        String ss = s; 
        String tmpnewchar1 = ""; 
        String[] part = ss.split("\\.");  
        if (ss.indexOf(".") != -1) { 
            if (part[1].length() > 2) { 
                //如果超出2位,系统自动截断 
                part[1] = part[1].substring(0, 2); 
                } 
            for (int i = 0; i < part[1].length(); i++) {
                 char perchar = part[1].charAt(i); 
                 if (perchar == '0') 
                 tmpnewchar1 = tmpnewchar1 + "零"; 
                 if (perchar == '1') 
                 tmpnewchar1 = tmpnewchar1 + "壹"; 
                 if (perchar == '2') 
                 tmpnewchar1 = tmpnewchar1 + "贰"; 
                 if (perchar == '3') 
                 tmpnewchar1 = tmpnewchar1 + "叁"; 
                 if (perchar == '4') 
                 tmpnewchar1 = tmpnewchar1 + "肆"; 
                 if (perchar == '5') 
                 tmpnewchar1 = tmpnewchar1 + "伍"; 
                 if (perchar == '6') 
                 tmpnewchar1 = tmpnewchar1 + "陆"; 
                 if (perchar == '7') 
                 tmpnewchar1 = tmpnewchar1 + "柒"; 
                 if (perchar == '8') 
                 tmpnewchar1 = tmpnewchar1 + "捌"; 
                 if (perchar == '9') 
                 tmpnewchar1 = tmpnewchar1 + "玖";  
                 if (i == 0 && perchar != 0) 
                 tmpnewchar1 = tmpnewchar1 + "角"; 
                 if (i == 1 && perchar != 0) 
                 tmpnewchar1 = tmpnewchar1 + "分"; 
             }        
          } 
     return tmpnewchar1;     
     } 
}

猜你喜欢

转载自blog.csdn.net/genaro26/article/details/51170183