金钱大写工具类

**

金钱大写工具类

**

/**
 *  @describe: 金钱大写工具类
 *  @author:houkai@lykj.com
 *  @Date: 2018/5/2 20:00
 *  `@version 1.0
 */
public class MoneyUtil {
    private static final String UNIT[] = { "万", "千", "佰", "拾", "亿", "千", "佰",
            "拾", "万", "千", "佰", "拾", "元", "角", "分" };

    private static final String NUM[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆",
            "柒", "捌", "玖" };

    private static final double MAX_VALUE = 9999999999999.99D;

    /**
     * 将金额小数转换成中文大写金额
     * @param money
     * @return result
     */
    public static String convertMoney(double money) {
        if (money < 0 || money > MAX_VALUE)
            return "参数非法!";
        long money1 = Math.round(money * 100); // 四舍五入到分
        if (money1 == 0)
            return "零元整";
        String strMoney = String.valueOf(money1);
        int numIndex = 0; // numIndex用于选择金额数值
        int unitIndex = UNIT.length - strMoney.length(); // unitIndex用于选择金额单位
        boolean isZero = false; // 用于判断当前为是否为零
        String result = "";
        for (; numIndex < strMoney.length(); numIndex++, unitIndex++) {
            char num = strMoney.charAt(numIndex);
            if (num == '0') {
                isZero = true;
                if (UNIT[unitIndex] == "亿" || UNIT[unitIndex] == "万"
                        || UNIT[unitIndex] == "元") { // 如果当前位是亿、万、元,且数值为零
                    result = result + UNIT[unitIndex]; //补单位亿、万、元
                    isZero = false;
                }
            }else {
                if (isZero) {
                    result = result + "零";
                    isZero = false;
                }
                result = result + NUM[Integer.parseInt(String.valueOf(num))] + UNIT[unitIndex];
            }
        }
        //不是角分结尾就加"整"字
        if (!result.endsWith("角")&&!result.endsWith("分")) {
            result = result + "整";
        }
        //例如没有这行代码,数值"400000001101.2",输出就是"肆千亿万壹千壹佰零壹元贰角"
        result = result.replaceAll("亿万", "亿");
        return result;
    }

    public static void main(String[] args){
          System.out.println(convertMoney(122304343.91));
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值