BigDecimalUtil工具类

需求

平常项目中很多时候需要我们做运算,那必不可少的要用到 BigDecimal 这个类,比如相除保留几位小数,判断是否为0,小数转百分数等等需求。下面是BigDecimalUtil工具类。方便我们开发

完整代码

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;

public class BigDecimalUtil {

    /**
     * 两数相除
     * 
     * @param a
     * @param b
     * @param scale
     * @return
     */

    public static BigDecimal divide(Long a, Long b, Integer scale) {
        BigDecimal bigDecimalA = new BigDecimal(a);
        BigDecimal bigDecimalB = new BigDecimal(b);
        return bigDecimalA.divide(bigDecimalB, scale,BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal divide(BigDecimal a, BigDecimal b, Integer scale) {
        if (b.compareTo(BigDecimal.ZERO) == 0) {
            throw new LazChoicePubChannelException(ErrorCode.PARAMETER_ERROR, "当前操作涉及运算,分母为0不允许通过,请重试");
        }
        return a.divide(b, scale, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 小数转百分数
     * 
     * @param decimal
     * @return
     */

    public static String decimal2Percentage(BigDecimal decimal) {
        return decimal2Percentage(decimal, 0);
    }
    public static String decimal2Percentage(String val) {
        return decimal2Percentage(new BigDecimal(val), 2);
    }
    public static String decimal2Percentage(String val, Integer scale) {
        return decimal2Percentage(new BigDecimal(val), scale);
    }

    public static String decimal2Percentage(BigDecimal decimal, Integer scale) {
        NumberFormat percentInstance = NumberFormat.getPercentInstance();
        // 保留几位百分数
        percentInstance.setMinimumFractionDigits(scale);
        return percentInstance.format(decimal.doubleValue());
    }

    /**
     * 保留指定小数 四舍五入
     * 
     * @param decimal
     * @param scale
     * @return
     */

    public static BigDecimal keepDecimals(BigDecimal decimal, Integer scale) {
        return decimal.setScale(scale, BigDecimal.ROUND_HALF_UP);
    }
    public static BigDecimal keepDecimals(String decimal, Integer scale) {
        return new BigDecimal(decimal).setScale(scale, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 把百分比数转化成对应的小数
     *
     * @param percentageStr 需要转化的百分比字符串
     * @return 转化后的小数
     */

    public static BigDecimal getPercentageToDecimal(String percentageStr, Integer scale) {
        String replace = percentageStr.replace("%""");
        return divide(new BigDecimal(replace), new BigDecimal(100), scale);
    }

    /**
     * 安全加法-忽略为空,为空视为0
     * @param a
     * @param b
     * @return
     */

    public static BigDecimal safeAdd(BigDecimal a, BigDecimal b) {
        if (a == null) a = BigDecimal.ZERO;
        if (b == null) b = BigDecimal.ZERO;
        return a.add(b);
    }

    /**
     * 判断是否为0
     * @param a
     * @return
     */

    public static Boolean isZero(BigDecimal a) {
        return a == null || BigDecimal.ZERO.compareTo(a) == 0;
    }
    public static Boolean isZero(String a) {
        return StringUtils.isBlank(a) ||  isZero(new BigDecimal(a));
    }
}


原文始发于微信公众号(干货食堂):BigDecimalUtil工具类

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/258487.html

(0)
葫芦侠五楼的头像葫芦侠五楼

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!