JAVA金额大小写转换

package com.idbp.common.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;


/**
 * @deccription 金额处理
 * @author zyh
 * @date 2016年12月8日
 */
public class MoneyUtils {


    /**
     * 汉语中数字大写
     */
    private static final String[] CN_UPPER_NUMBER = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
    /**
     * 汉语中货币单位大写,这样的设计类似于占位符
     */
    private static final String[] CN_UPPER_MONETRAY_UNIT = { "分", "角", "元",
            "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾",
            "佰", "仟" };
    /**
     * 特殊字符:整
     */
    private static final String CN_FULL = "整";
    /**
     * 特殊字符:负
     */
    private static final String CN_NEGATIVE = "负";
    /**
     * 金额的精度,默认值为2
     */
    private static final int MONEY_PRECISION = 2;
    /**
     * 特殊字符:零元整
     */
    private static final String CN_ZEOR_FULL = "零元" + CN_FULL;
    /**
     * 四舍五入
     */
    public static final RoundingMode HALF_UP = RoundingMode.HALF_UP;
    /**
     * 向上取整
     */
    public static final RoundingMode UP = RoundingMode.UP;


    /**
     * @deccription 以元为单位的金额转换成分
     * @param yuan
     * @param decimal
     * @return
     * @return BigDecimal
     */
    public static BigDecimal transferY2F(BigDecimal yuan, int decimal) {

        StringBuffer transAmt = new StringBuffer("1");
        while (decimal > 0) {
            transAmt.append("0");
            decimal--;
        }

        if (yuan != null) {
            return multiply(yuan, new BigDecimal(transAmt.toString()), RoundingMode.DOWN, null).setScale(0);
        }

        return BigDecimal.ZERO;
    }


    /**
     * @deccription 以分为单位的金额转换成以元为单位
     * @param fen
     * @param decimal
     * @return
     * @return BigDecimal
     */
    public static BigDecimal transferF2Y(BigDecimal fen, int decimal) {

        StringBuffer transAmt = new StringBuffer("1");
        while (decimal > 0) {
            transAmt.append("0");
            decimal--;
        }

        if (fen != null) {
            return divide(fen, new BigDecimal(transAmt.toString()), RoundingMode.DOWN, null);
        }

        return BigDecimal.ZERO;
    }


    /**
     * @deccription 金额乘法
     * @param first
     * @param second
     * @param roundingMode
     *            默认四舍五入
     * @param scale
     *            默认2位小数
     * @return
     * @return BigDecimal
     */
    public static BigDecimal multiply(BigDecimal first, BigDecimal second, RoundingMode roundingMode,
            Integer scale) {
        if (roundingMode == null) {
            roundingMode = MoneyUtils.HALF_UP;
        }
        if (first == null)
            first = BigDecimal.ZERO;
        if (second == null)
            second = BigDecimal.ZERO;
        if (scale == null)
            scale = 2;
        return (first.multiply(second)).setScale(scale, roundingMode);
    }


    /**
     * @deccription 金额除法
     * @param first
     * @param second
     * @param roundingMode
     *            默认四舍五入
     * @param scale
     *            默认2位小数
     * @return
     * @return BigDecimal
     */
    public static BigDecimal divide(BigDecimal first, BigDecimal second, RoundingMode roundingMode,
            Integer scale) {
        if (roundingMode == null) {
            roundingMode = MoneyUtils.HALF_UP;
        }
        if (first == null)
            first = BigDecimal.ZERO;
        if (second == null)
            second = BigDecimal.ZERO;
        if (scale == null)
            scale = 2;
        return first.divide(second, scale, roundingMode);
    }


    /**
     * @deccription 金额减法
     * @param first
     * @param second
     * @param roundingMode
     *            默认四舍五入
     * @param scale
     *            默认2位小数
     * @return
     * @return BigDecimal
     */
    public static BigDecimal subtract(BigDecimal first, BigDecimal second, RoundingMode roundingMode,
            Integer scale) {
        if (roundingMode == null) {
            roundingMode = MoneyUtils.HALF_UP;
        }
        if (first == null)
            first = BigDecimal.ZERO;
        if (second == null)
            second = BigDecimal.ZERO;
        if (scale == null)
            scale = 2;
        return (first.subtract(second)).setScale(scale, roundingMode);
    }


    /**
     * @deccription 金额加法
     * @param first
     * @param second
     * @param roundingMode
     *            默认四舍五入
     * @param scale
     *            默认2位小数
     * @return
     * @return BigDecimal
     */
    public static BigDecimal add(BigDecimal first, BigDecimal second, RoundingMode roundingMode, Integer scale) {
        if (roundingMode == null) {
            roundingMode = MoneyUtils.HALF_UP;
        }
        if (first == null)
            first = BigDecimal.ZERO;
        if (second == null)
            second = BigDecimal.ZERO;
        if (scale == null)
            scale = 2;
        return (first.add(second)).setScale(scale, roundingMode);
    }


    /**
     * @deccription 获取随机的0.01元至配置最高金额的之间的金额
     * @param maxTransferAmt
     * @return
     * @return BigDecimal
     */
    public static BigDecimal randomTransferAmt(BigDecimal maxTransferAmt) {
        return MoneyUtils.divide(
            MoneyUtils.multiply(maxTransferAmt, new BigDecimal(new Random().nextInt(100)), null, null),
            new BigDecimal(100), null, null);
    }


    /**
     * @deccription 判断金额是否为100的整数倍
     * @param BigDecimal
     * @return
     * @return boolean
     */
    public static boolean is100zs(BigDecimal txnAmt) {
        boolean flag = false;
        String amt = txnAmt.divide(new BigDecimal(100)).toString();
        for (int i = amt.length(); --i >= 0;) {
            if (!Character.isDigit(amt.charAt(i))) {
                flag = true;
            }
        }
        return flag;
    }


    /**
     * @deccription 获取英式金额显示结果
     * @param decimal
     * @return
     * @return String
     */
    public static String EnMoneyFormat(BigDecimal decimal) {
        String str = decimal.toString();
        String prefix = str;
        if (str.contains(".")) {
            prefix = str.substring(0, str.indexOf("."));
        }
        StringBuffer sb = new StringBuffer();
        int j = 1;
        for (int i = prefix.length() - 1; i >= 0; i--) {
            sb.insert(0, String.valueOf(prefix.charAt(i)));
            if (j % 3 == 0 && j != prefix.length()) {
                sb.insert(0, ",");
            }
            j++;
        }
        if (str.contains(".")) {
            sb.append(str.subSequence(str.indexOf("."), str.length()));
        }

        return sb.toString();
    }

    /**
     * 把输入的金额转换为汉语中人民币的大写
     *
     * @param numberOfMoney
     *            输入的金额
     * @return 对应的汉语大写
     */
    public static String number2CNMontrayUnit(BigDecimal numberOfMoney) {
        StringBuffer sb = new StringBuffer();
        // -1, 0, or 1 as the value of this BigDecimal is negative, zero, or
        // positive.
        int signum = numberOfMoney.signum();
        // 零元整的情况
        if (signum == 0) {
            return CN_ZEOR_FULL;
        }
        // 这里会进行金额的四舍五入
        long number = numberOfMoney.movePointRight(MONEY_PRECISION)
                .setScale(0, 4).abs().longValue();
        // 得到小数点后两位值
        long scale = number % 100;
        int numUnit = 0;
        int numIndex = 0;
        boolean getZero = false;
        // 判断最后两位数,一共有四中情况:00 = 0, 01 = 1, 10, 11
        if (!(scale > 0)) {
            numIndex = 2;
            number = number / 100;
            getZero = true;
        }
        if ((scale > 0) && (!(scale % 10 > 0))) {
            numIndex = 1;
            number = number / 10;
            getZero = true;
        }
        int zeroSize = 0;
        while (true) {
            if (number <= 0) {
                break;
            }
            // 每次获取到最后一个数
            numUnit = (int) (number % 10);
            if (numUnit > 0) {
                if ((numIndex == 9) && (zeroSize >= 3)) {
                    sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
                }
                if ((numIndex == 13) && (zeroSize >= 3)) {
                    sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
                }
                sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
                sb.insert(0, CN_UPPER_NUMBER[numUnit]);
                getZero = false;
                zeroSize = 0;
            } else {
                ++zeroSize;
                if (!(getZero)) {
                    sb.insert(0, CN_UPPER_NUMBER[numUnit]);
                }
                if (numIndex == 2) {
                    if (number > 0) {
                        sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
                    }
                } else if (((numIndex - 2) % 4 == 0) && (number % 1000 > 0)) {
                    sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
                }
                getZero = true;
            }
            // 让number每次都去掉最后一个数
            number = number / 10;
            ++numIndex;
        }
        // 如果signum == -1,则说明输入的数字为负数,就在最前面追加特殊字符:负
        if (signum == -1) {
            sb.insert(0, CN_NEGATIVE);
        }
        // 输入的数字小数点后两位为"00"的情况,则要在最后追加特殊字符:整
        if (!(scale > 0)) {
            sb.append(CN_FULL);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(is100zs(new BigDecimal("200")));
    }

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值