Java 将小写金额转换成大写金额

将小写金额转换成大写金额
例如:110.00转换后为 壹佰壹拾圆整
代码如下

/**
     * 将小写金额转换为大写
     * 
     * @param amount
     *            110.00
     * @return 壹佰壹拾圆整
     */
    public static String amountToUpper(String amount) throws Exception {
        // String[] lowerAmount = { "0", "1", "2", "3", "4", "5", "6", "7", "8",
        // "9" };
        String[] upperAmount = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌",
                "玖" };
        String[] unit = { "分", "角", "圆", "拾", "佰", "仟", "萬", "亿" };
        String wholeUnit = "整";
        StringBuffer result = new StringBuffer();

        if (StringUtil.isEmpty(amount)) { return ""; }
        // 为0
        if (Double.valueOf(amount) == 0) { return upperAmount[0]; }
        // 去掉开头和结尾的0
        amount = amount.replaceAll("^0*", "");
        if (amount.startsWith(".")) {
            amount = "0" + amount;
        }
        if (amount.indexOf(".") > -1) {
            amount = amount.replaceAll("0*$|\\.0{1,2}$", "");
        }
        // 判断格式
        Pattern p = Pattern.compile("\\d{1,12}(\\.\\d{1,2})?");
        Matcher m = p.matcher(amount);
        if (!m.matches()) { throw new Exception("金额格式不正确! "); }
        // 分成整数和小数分别读
        String whole = "";
        String integral = "";
        if (amount.indexOf(".") > -1) {
            whole = amount.split("\\.")[0];
            integral = amount.split("\\.")[1];
        } else {
            whole = amount;
        }
        // 整数读法
        StringBuffer sceAmount = new StringBuffer(whole);
        if (sceAmount.length() > 4) { // 每4位用逗号分隔
            int count = sceAmount.length() / 4;
            for (int i = 0; i <= count; i++) {
                if (i == 1) {
                    sceAmount.insert(sceAmount.length() - 4 * i, ",");
                } else if (i > 1) {
                    sceAmount.insert(sceAmount.length() - 4 * i - 1, ",");
                }
            }
        }
        String[] sce = sceAmount.toString().split(",");
        for (int i = sce.length - 1; i >= 0; i--) { // 每4位循环读
            StringBuffer oneComma = new StringBuffer();
            if (Pattern.compile("\\d{1,4}").matcher(sce[i]).matches()) {
                if (Pattern.compile("[1-9]{4}").matcher(sce[i]).matches()) { // 不含有0
                    int t = Integer.valueOf(String.valueOf(sce[i].charAt(0))); // 千位
                    int h = Integer.valueOf(String.valueOf(sce[i].charAt(1))); // 百位
                    int d = Integer.valueOf(String.valueOf(sce[i].charAt(2))); // 十位
                    int e = Integer.valueOf(String.valueOf(sce[i].charAt(3))); // 个位
                    oneComma.append(upperAmount[t])
                            .append(unit[5])
                            .append(upperAmount[h])
                            .append(unit[4])
                            .append(upperAmount[d])
                            .append(unit[3])
                            .append(upperAmount[e]);
                } else if (Pattern  .compile("0{1}[1-9]{1}0{1}[1-9]{1}")
                                    .matcher(sce[i])
                                    .matches()) {
                    int h = Integer.valueOf(String.valueOf(sce[i].charAt(1))); // 百位
                    int e = Integer.valueOf(String.valueOf(sce[i].charAt(3))); // 个位
                    oneComma.append(upperAmount[0])
                            .append(upperAmount[h])
                            .append(unit[4])
                            .append(upperAmount[0])
                            .append(upperAmount[e]);
                } else if (Pattern  .compile("0{1,3}[1-9]{1}")
                                    .matcher(sce[i])
                                    .matches()) {
                    int e = Integer.valueOf(String.valueOf(sce[i].charAt(3))); // 个位
                    oneComma.append(upperAmount[0]).append(upperAmount[e]);
                } else if (Pattern  .compile("0{1,2}[1-9]{1}0{1}")
                                    .matcher(sce[i])
                                    .matches()) {
                    int d = Integer.valueOf(String.valueOf(sce[i].charAt(2))); // 十位
                    oneComma.append(upperAmount[0])
                            .append(upperAmount[d])
                            .append(unit[3]);
                } else if (Pattern  .compile("0{1}[1-9]{1}0{2}")
                                    .matcher(sce[i])
                                    .matches()) {
                    int h = Integer.valueOf(String.valueOf(sce[i].charAt(1))); // 百位
                    oneComma.append(upperAmount[0])
                            .append(upperAmount[h])
                            .append(unit[4]);
                } else if (Pattern  .compile("[1-9]{1}0{3}")
                                    .matcher(sce[i])
                                    .matches()) {
                    int t = Integer.valueOf(String.valueOf(sce[i].charAt(0))); // 千位
                    oneComma.append(upperAmount[t]).append(unit[5]);
                } else if (Pattern  .compile("[1-9]{2}0{2}")
                                    .matcher(sce[i])
                                    .matches()) {
                    int t = Integer.valueOf(String.valueOf(sce[i].charAt(0))); // 千位
                    int h = Integer.valueOf(String.valueOf(sce[i].charAt(1))); // 百位
                    oneComma.append(upperAmount[t])
                            .append(unit[5])
                            .append(upperAmount[h])
                            .append(unit[4]);
                } else if (Pattern  .compile("[1-9]{1}0{1}[1-9]{1}0{1}")
                                    .matcher(sce[i])
                                    .matches()) {
                    int t = Integer.valueOf(String.valueOf(sce[i].charAt(0))); // 千位
                    int d = Integer.valueOf(String.valueOf(sce[i].charAt(2))); // 十位
                    oneComma.append(upperAmount[t])
                            .append(unit[5])
                            .append(upperAmount[0])
                            .append(upperAmount[d])
                            .append(unit[3]);
                } else if (Pattern  .compile("[1-9]{1}0{2}[1-9]{1}")
                                    .matcher(sce[i])
                                    .matches()) {
                    int t = Integer.valueOf(String.valueOf(sce[i].charAt(0))); // 千位
                    int e = Integer.valueOf(String.valueOf(sce[i].charAt(3))); // 个位
                    oneComma.append(upperAmount[t])
                            .append(unit[5])
                            .append(upperAmount[0])
                            .append(upperAmount[e]);
                } else if (Pattern  .compile("0{1}[1-9]{2}0{1}")
                                    .matcher(sce[i])
                                    .matches()) {
                    int h = Integer.valueOf(String.valueOf(sce[i].charAt(1))); // 百位
                    int d = Integer.valueOf(String.valueOf(sce[i].charAt(2))); // 十位
                    oneComma.append(upperAmount[0])
                            .append(upperAmount[h])
                            .append(unit[4])
                            .append(upperAmount[d])
                            .append(unit[3]);
                } else if (Pattern  .compile("0{1,2}[1-9]{2}")
                                    .matcher(sce[i])
                                    .matches()) {
                    int d = Integer.valueOf(String.valueOf(sce[i].charAt(2))); // 十位
                    int e = Integer.valueOf(String.valueOf(sce[i].charAt(3))); // 个位
                    oneComma.append(upperAmount[0])
                            .append(upperAmount[d])
                            .append(unit[3])
                            .append(upperAmount[e]);
                } else if (Pattern  .compile("[1-9]{3}0{1}")
                                    .matcher(sce[i])
                                    .matches()) {
                    int t = Integer.valueOf(String.valueOf(sce[i].charAt(0))); // 千位
                    int h = Integer.valueOf(String.valueOf(sce[i].charAt(1))); // 百位
                    int d = Integer.valueOf(String.valueOf(sce[i].charAt(2))); // 十位
                    oneComma.append(upperAmount[t])
                            .append(unit[5])
                            .append(upperAmount[h])
                            .append(unit[4])
                            .append(upperAmount[d])
                            .append(unit[3]);
                } else if (Pattern  .compile("[1-9]{2}0{1}[1-9]{1}")
                                    .matcher(sce[i])
                                    .matches()) {
                    int t = Integer.valueOf(String.valueOf(sce[i].charAt(0))); // 千位
                    int h = Integer.valueOf(String.valueOf(sce[i].charAt(1))); // 百位
                    int e = Integer.valueOf(String.valueOf(sce[i].charAt(3))); // 个位
                    oneComma.append(upperAmount[t])
                            .append(unit[5])
                            .append(upperAmount[h])
                            .append(unit[4])
                            .append(upperAmount[0])
                            .append(upperAmount[e]);
                } else if (Pattern  .compile("0{1}[1-9]{3}")
                                    .matcher(sce[i])
                                    .matches()) { // 四位
                    int h = Integer.valueOf(String.valueOf(sce[i].charAt(1))); // 百位
                    int d = Integer.valueOf(String.valueOf(sce[i].charAt(2))); // 十位
                    int e = Integer.valueOf(String.valueOf(sce[i].charAt(3))); // 个位
                    oneComma.append(upperAmount[0])
                            .append(upperAmount[h])
                            .append(unit[4])
                            .append(upperAmount[d])
                            .append(unit[3])
                            .append(upperAmount[e]);
                } else if (Pattern  .compile("[1-9]{1}0{1}[1-9]{2}")
                                    .matcher(sce[i])
                                    .matches()) {
                    int t = Integer.valueOf(String.valueOf(sce[i].charAt(0))); // 千位
                    int d = Integer.valueOf(String.valueOf(sce[i].charAt(2))); // 十位
                    int e = Integer.valueOf(String.valueOf(sce[i].charAt(3))); // 个位
                    oneComma.append(upperAmount[t])
                            .append(unit[5])
                            .append(upperAmount[0])
                            .append(upperAmount[d])
                            .append(unit[3])
                            .append(upperAmount[e]);
                } else if (Pattern  .compile("[1-9]{3}")
                                    .matcher(sce[i])
                                    .matches()) { // 三位
                    int h = Integer.valueOf(String.valueOf(sce[i].charAt(0))); // 百位
                    int d = Integer.valueOf(String.valueOf(sce[i].charAt(1))); // 十位
                    int e = Integer.valueOf(String.valueOf(sce[i].charAt(2))); // 个位
                    oneComma.append(upperAmount[h])
                            .append(unit[4])
                            .append(upperAmount[d])
                            .append(unit[3])
                            .append(upperAmount[e]);
                } else if (Pattern  .compile("[1-9]{1}0{2}")
                                    .matcher(sce[i])
                                    .matches()) { // 三位
                    int h = Integer.valueOf(String.valueOf(sce[i].charAt(0))); // 百位
                    oneComma.append(upperAmount[h]).append(unit[4]);
                } else if (Pattern  .compile("[1-9]{1}0{1}[1-9]{1}")
                                    .matcher(sce[i])
                                    .matches()) {
                    int h = Integer.valueOf(String.valueOf(sce[i].charAt(0))); // 百位
                    int e = Integer.valueOf(String.valueOf(sce[i].charAt(2))); // 个位
                    oneComma.append(upperAmount[h])
                            .append(unit[4])
                            .append(upperAmount[0])
                            .append(upperAmount[e]);
                } else if (Pattern  .compile("[1-9]{2}0{1}")
                                    .matcher(sce[i])
                                    .matches()) {
                    int h = Integer.valueOf(String.valueOf(sce[i].charAt(0))); // 百位
                    int d = Integer.valueOf(String.valueOf(sce[i].charAt(1))); // 十位
                    oneComma.append(upperAmount[h])
                            .append(unit[4])
                            .append(upperAmount[d])
                            .append(unit[3]);
                } else if (Pattern  .compile("[1-9]{1}")
                                    .matcher(sce[i])
                                    .matches()) {
                    int e = Integer.valueOf(String.valueOf(sce[i].charAt(0))); // 个位
                    oneComma.append(upperAmount[e]);
                } else if (Pattern  .compile("[1-9]{2}")
                                    .matcher(sce[i])
                                    .matches()) { // 两位
                    int d = Integer.valueOf(String.valueOf(sce[i].charAt(0))); // 十位
                    int e = Integer.valueOf(String.valueOf(sce[i].charAt(1))); // 个位
                    oneComma.append(upperAmount[d])
                            .append(unit[3])
                            .append(upperAmount[e]);
                } else if (Pattern  .compile("[1-9]{1}0{1}")
                                    .matcher(sce[i])
                                    .matches()) {
                    int d = Integer.valueOf(String.valueOf(sce[i].charAt(0))); // 十位
                    oneComma.append(upperAmount[d]).append(unit[3]);
                }
            }
            if (sce.length == 3) {
                if (i == 2) {
                    result.insert(0, oneComma.toString()).append(unit[2]);
                } else if (i == 1) {// 有一个逗号(包含万位单位)
                    oneComma.append(unit[6]);
                    result.insert(0, oneComma.toString());
                } else if (i == 0) { // 有两个逗号(包含亿位单位)
                    oneComma.append(unit[7]);
                    result.insert(0, oneComma.toString());
                }
            } else if (sce.length == 2) {
                if (i == 1) {
                    result.insert(0, oneComma.toString()).append(unit[2]);
                } else if (i == 0) {// 有一个逗号(包含万位单位)
                    oneComma.append(unit[6]);
                    result.insert(0, oneComma.toString());
                }
            } else if (sce.length == 1) {// 没有逗号,最大单位为千
                result.insert(0, oneComma.toString()).append(unit[2]);
            }
        }
        // 小数读法
        if (integral.length() == 1) { // 只带角
            result  .append(upperAmount[0])
                    .append(upperAmount[Integer.valueOf(integral)])
                    .append(unit[1]);
        } else if (integral.length() == 2) {
            if (!integral.startsWith("0")) {// 有角有分
                result  .append(upperAmount[0])
                        .append(upperAmount[Integer.valueOf(String.valueOf(integral.charAt(0)))])
                        .append(unit[1])
                        .append(upperAmount[Integer.valueOf(String.valueOf(integral.charAt(1)))])
                        .append(unit[0]);
            } else {// 只有分
                result  .append(upperAmount[0])
                        .append(upperAmount[Integer.valueOf(String.valueOf(integral.charAt(1)))])
                        .append(unit[0]);
            }
        } else if (integral.equals("")) {
            result.append(wholeUnit);
        }
        return result.toString();
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
//ChangeRMB.java /** * * programmed by HuangHeliang * 2009.04.15 10:20:51 * */ //package com.avtech.hhl; import java.io.*; public final class ChangeRMB { //每个数字对应的大写 private static final String[] num = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", }; //从低到高排列的单位 private static final String[] bit = { "圆", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿" }; //金额里面的角和分 private static final String[] jf={ "角","分" }; /** * 处理金额的整数部分,返回"...圆整" * @param integer * @return String * @throws Exception */ public static String praseUpcaseRMB(String integer)throws Exception{ StringBuilder sbdr=new StringBuilder(""); int j=integer.length(); if(j>bit.length){ throw new Exception("\n只能处理亿万亿以内的数据(含亿万亿)!"); } char[] rmb=integer.toCharArray(); for (int i = 0; i 壹佰亿陆仟伍佰万肆仟伍佰捌拾叁圆零伍分 */ if(bit[bitLocate].equals("仟")){ String s=sbdr.toString(); if(!s.endsWith(bit[bitLocate+1]) && s.length()>0){ if (s.endsWith(num[0])) { sbdr.deleteCharAt(sbdr.length() - 1); } sbdr.append(bit[bitLocate+1]); } } sbdr.append(num[numLocate]); sbdr.append(bit[bitLocate]); }//end for /* * 去掉结尾"零"后,补全 */ if(sbdr.toString().endsWith(num[0])){ sbdr.deleteCharAt(sbdr.length()-1); sbdr.append("圆整"); }else{ sbdr.append("整"); } return sbdr.toString(); } /** * 处理带小数的金额,整数部分交由上一个方法处理,小数部分自己处理 * @param integer * @param decimal * @return String * @throws Exception */ public static String praseUpcaseRMB(String integer, String decimal)throws Exception{ String ret=ChangeRMB.praseUpcaseRMB(integer); ret=ret.split("整")[0]; //处理整数部分 StringBuilder sbdr=new StringBuilder(""); sbdr.append(ret); char[] rmbjf=decimal.toCharArray(); for(int i=0;i rmbDouble){ theInt-=1; } double theDecimal=rmbDouble-theInt; String integer=new Long((long)theInt).toString(); String decimal=""+Math.round(theDecimal*100); if(decimal.equals("0")){ result=ChangeRMB.praseUpcaseRMB(integer); }else{ result=ChangeRMB.praseUpcaseRMB(integer, decimal); } return result; } public static void main(String[] args) throws Exception{ System.out.print("输入小写人民币金额:"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String in = br.readLine(); String result=ChangeRMB.doChangeRMB(in); System.out.println("\n"+"------------转换结果------------"); System.out.println(result); double d=54628569856.68; String ret=ChangeRMB.doChangeRMB(d); System.out.println("\n"+"------------转换结果------------"); System.out.println(ret); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值