金额转换 NumberUtils

package com.zhong.dm.api.utils;


import java.math.BigDecimal;
import java.text.DecimalFormat;


/**
 * 金额数字工具类
 */
public class NumberUtils {


    private final static BigDecimal HUNDRED_MILLION = new BigDecimal("100000000");
    private final static BigDecimal HUNDRED = new BigDecimal("100");
    private final static BigDecimal TEN_THOUSAND = new BigDecimal("10000");


    private NumberUtils() {
    }


    /**
     * 单位换算: 将(元)转化为(亿元),主要用于UI显示
     */
    public static String getHundredMillionStr(BigDecimal money) {
        return getHundredMillionStr(money, "");
    }


    /**
     * 单位换算: 将(元)转化为(万元),主要用于UI显示
     */
    public static String getTenThousandStr(BigDecimal money) {
        return getHundredMillionStr(money, "");
    }


    /**
     * 单位换算: 将(元)转化为(亿元),主要用于UI显示
     *
     * @defaultValue 当money参数为null时,默认显示的值
     */
    public static String getHundredMillionStr(BigDecimal money, String defaultValue) {
        if (money == null) {
            return defaultValue;
        }
        BigDecimal value = money.divide(HUNDRED_MILLION);
        DecimalFormat df = new DecimalFormat("#,###.################");
        String format = df.format(value);
        return format;
    }
    public static void main(String[] args) {
System.out.println(getHundredMillionStr(new BigDecimal("1.25")));
}


    /**
     * 单位换算: 将(元)转化为(万元),主要用于UI显示
     *
     * @defaultValue 当money参数为null时,默认显示的值
     */
    public static String getTenThousandStr(BigDecimal money, String defaultValue) {
        if (money == null) {
            return defaultValue;
        }
        BigDecimal value = money.divide(TEN_THOUSAND);
        DecimalFormat df = new DecimalFormat("#,###.################");
        String format = df.format(value);
        return format;
    }


    /**
     * 单位换算: 将(元)转化为(亿元)
     */
    public static BigDecimal getHundredMillionBigDecimal(BigDecimal money) {
        if (money == null) {
            return null;
        }
        return money.divide(HUNDRED_MILLION, 10, BigDecimal.ROUND_DOWN);
    }


    /**
     * 单位换算: 将(元)转化为(万元)
     */
    public static BigDecimal getTenThousandBigDecimal(BigDecimal money) {
        if (money == null) {
            return null;
        }
        return money.divide(TEN_THOUSAND, 10, BigDecimal.ROUND_DOWN);
    }
    // String expectedFinAmtStr = dto.getExpectedFinAmt().compareTo(BigDecimal.ZERO) == 0 ? "0"
    // : dto.getExpectedFinAmt().divide(new BigDecimal("100000000"), 10,
    // BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString();


    /**
     * 单位换算: 将0.2345 乘 100 转化为 23.45 (百分比)
     */
    public static BigDecimal getRate(BigDecimal b) {
        return b == null ? null : b.multiply(HUNDRED);
    }


    public static String getRateStr(BigDecimal b) {
        return toPlainString(getRate(b));
    }


    public static String toPlainString(BigDecimal b) {
        return b == null ? null : b.stripTrailingZeros().toPlainString();
    }


    public static String toPlainString(BigDecimal b, String defaultStr) {
        return b == null ? defaultStr : b.stripTrailingZeros().toPlainString();
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值