首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

人民币金额大写有关问题

2012-04-01 
人民币金额大写问题从键盘输入一个十亿以内的正整数(int类型),把它转换为人民币金额大写比如用户输入:3020

人民币金额大写问题
从键盘输入一个十亿以内的正整数(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 + "");    }} 


[解决办法]
我以前写了个简单的,给你找出来了,支持13位,checkinput做得不好,你拿去改一下。

Java code
/** * 转换金额为大写 *  * @author dryzeng * @version 20081124 */public class MoneyFormat {        private StringBuffer sb = new StringBuffer();    private String bigLetter[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌","玖" };    private String unit[] = { "圆", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾","佰", "仟", "万" };    /**     * 转换金额为大写     *      * @param String 小写金额     * @return String 大写金额     * @author dryzeng     */    public String format(String inputMoney) {        if (inputMoney.length() > 13) {            sb.append("数字太大,无法显示!");            return sb.toString();        } else if (Double.valueOf(inputMoney) < 0) {            sb.append("错误,金额为负数!");            return sb.toString();        } else if (Double.valueOf(inputMoney) == 0) {            sb.append("零圆整");            return sb.toString();        }        // 取得小数点前的数字        String moneyf[] = String.valueOf(inputMoney).split("\\.")[0].split("");        //金额转换        for (int i = 1; i < moneyf.length; i++) {            sb.append(bigLetter[Integer.valueOf(moneyf[i])]);            sb.append(unit[moneyf.length - 1 - i]);        }        try {            // 取得小数点后的数字            String moneyb[] = String.valueOf(inputMoney).split("\\.")[1].split("");            sb.append(bigLetter[Integer.valueOf(moneyb[1])]);            sb.append("角");        } catch (Exception e) {}        //格式人性化        String finalsb = sb.toString();        finalsb = finalsb.replaceAll("零仟", "零");        finalsb = finalsb.replaceAll("零佰", "零");        finalsb = finalsb.replaceAll("零拾", "零");        finalsb = finalsb.replaceAll("零亿", "亿");        finalsb = finalsb.replaceAll("零万", "万");        finalsb = finalsb.replaceAll("零零零", "零");        finalsb = finalsb.replaceAll("零零", "零");        finalsb = finalsb.replaceAll("零圆", "圆");        finalsb = finalsb.replaceAll("亿万", "亿");        finalsb = finalsb.replaceAll("零角", "");        if ("圆".equals(finalsb.substring(0, 1))) {            finalsb = "零" + finalsb;        }        return finalsb;    }} 

热点排行