日期:2014-05-20  浏览次数:20651 次

人民币金额大写问题
从键盘输入一个十亿以内的正整数(int类型),把它转换为人民币金额大写
比如
                  用户输入:30201,程序输出:叁万零贰佰零壹
用户输入:30001,程序输出:叁万零壹
用户输入:31000,程序输出:叁万壹仟
用户输入:120023201,程序输出:壹亿贰仟零贰万叁仟贰佰零壹
用户输入:120020001,程序输出:壹亿贰仟零贰万零壹
用户输入:100000001,程序输出:壹亿零壹
在万后满千位,则不加零,否则要补零,但不要出现类似“零零”的情况。
在亿后满千万位,则不加零,否则要补零,但整个“万档”没有数字时,“万”字省去。

------解决方案--------------------
回复了怎么没有....
网上很多的,自己写其实也没多难的。需要的话加我QQ12642323 回家发个给你
------解决方案--------------------
Java code

package com.demo;

import java.util.HashMap;
import java.util.Map;

public class Test2 {
    /**
     * @param args
     */
    public static void main(String[] args) {
        //int num = 190001;
        //String number = new Integer(num).toString();

        //System.out.println(number.length());
        //System.out.println(number.length() / 4);
        //System.out.println(number.length() % 4);
        
        int [] n = new int [] {30000,31000,120023201,120020001,100010001};

        for(int num : n){
            
            String number = new Integer(num).toString();
            System.out.println(slipNum(number));
        }
    }
    
    public static String slipNum(String formatNum){
        
        StringBuffer sb = new StringBuffer();
        if (formatNum.length() <= 4) {
            sb.append(format(formatNum));
        }else if (formatNum.length() <=8 ) {
            sb.append(format(formatNum.substring(0,formatNum.length()-4))).append("万").append(slipNum(formatNum.substring(formatNum.length()-4,formatNum.length())));
        }else if (formatNum.length() <=12 ) {
            sb.append(format(formatNum.substring(0,formatNum.length()-8))).append("亿").append(slipNum(formatNum.substring(formatNum.length()-8,formatNum.length())));
        }
        
        return sb.toString();
    }

    public static String format(String formatNum) {
        
        StringBuffer sb = new StringBuffer();
        
        if (formatNum.length() == 1)
            
            sb.append(getChinaseNumber(formatNum));
        
        else if (formatNum.length() == 2) {
            
            if (formatNum.endsWith("0"))
                sb.append(getChinaseNumber(formatNum));
            else
                sb.append(getChinaseNumber(formatNum.substring(0, 1))).append("十").append(getChinaseNumber(formatNum.substring(1,2)));
            
        } else if (formatNum.length() == 3){
            
            if (formatNum.startsWith("00"))
                sb.append(getChinaseNumber("00")).append(format(formatNum.substring(2,3)));
            else if (formatNum.startsWith("0"))
                sb.append(getChinaseNumber("0")).append(format(formatNum.substring(1,3)));
            else if (formatNum.endsWith("00"))
                sb.append(getChinaseNumber(formatNum));
            else if (formatNum.endsWith("0"))
                sb.append(format(formatNum.substring(0,1))).append("百").append(format(formatNum.substring(1,3)));    
            else if (formatNum.indexOf("0")!=-1)
                sb.append(getChinaseNumber(formatNum.substring(0, 1))).append("百").append(getChinaseNumber(formatNum.substring(1,2))).append(getChinaseNumber(formatNum.substring(2,3)));
            else 
                sb.append(getChinaseNumber(formatNum.substring(0, 1))).append("百").append(getChinaseNumber(formatNum.substring(1,2))).append("十").append(getChinaseNumber(formatNum.substring(2,3)));
            
        }else if (formatNum.length() == 4){
            
            if (formatNum.startsWith("0000"))
                sb.append("");
            else if (formatNum.startsWith("000"))
                sb.append(getChinaseNumber("000")).append(format(formatNum.substring(3,4)));
            else if (formatNum.startsWith("00"))
                sb.append(getChinaseNumber("00")).append(format(formatNum.substring(2,4)));
            else if (formatNum.startsWith("0"))
                sb.append(getChinaseNumber("0")).append(format(formatNum.substring(1,4)));
            else if (formatNum.endsWith("000"))
                sb.append(getChinaseNumber(formatNum));
            else if (formatNum.endsWith("00")||formatNum.endsWith("0"))
                sb.append(format(formatNum.substring(0,1))).append("千").append(format(formatNum.substring(1,4)));
            else if (formatNum.indexOf("00")!=-1)
                sb.append(getChinaseNumber(formatNum.substring(0, 1))).append("千").append(getChinaseNumber(formatNum.substring(1,3))).append(getChinaseNumber(formatNum.substring(3,4)));
            else if (formatNum.indexOf("0")!=-1)
                sb.append(getChinaseNumber(formatNum.substring(0, 1))).append("千").append(format(formatNum.substring(1,4)));
            else 
                sb.append(getChinaseNumber(formatNum.substring(0, 1))).append("千").append(getChinaseNumber(formatNum.substring(1, 2))).append("百").append(getChinaseNumber(formatNum.substring(2,3))).append("十").append(getChinaseNumber(formatNum.substring(4,4)));
    
        }

        return sb.toString();
    }

    public static String getChinaseNumber(String n) {

        Map<String, String> map = new HashMap();
        map.put("1", "一");
        map.put("2", "二");
        map.put("3", "三");
        map.put("4", "四");
        map.put("5", "五");
        map.put("6", "六");
        map.put("7", "七");
        map.put("8", "八");
        map.put("9", "九");

        map.put("10", "一十");
        map.put("100", "一百");
        map.put("1000", "一千");

        map.put("20", "二十");
        map.put("200", "二百");
        map.put("2000", "二千");

        map.put("30", "三十");
        map.put("300", "三百");
        map.put("3000", "三千");

        map.put("40", "四十");
        map.put("400", "四百");
        map.put("4000", "四千");

        map.put("50", "五十");
        map.put("500", "五百");
        map.put("5000", "五千");

        map.put("60", "六十");
        map.put("600", "六百");
        map.put("6000", "六千");

        map.put("70", "七十");
        map.put("700", "七百");
        map.put("7000", "七千");

        map.put("80", "八十");
        map.put("800", "八百");
        map.put("8000", "八千");

        map.put("90", "九十");
        map.put("900", "九百");
        map.put("9000", "九千");

        map.put("0", "零");
        map.put("00", "零");
        map.put("000", "零");
        map.put("0000", "零");

        return map.get(n + "");
    }

}