Java中BigDecimal 工具类

简介: Java中BigDecimal 工具类

public class BigDecimalUtil {

/**
 * 系统精度
 */
public static int scaleSystem = 2;

@Value("${lzb.basic.computationScale:2}")
public void setScaleSystem(int scaleCustom) {
    BigDecimalUtil.scaleSystem = scaleCustom;
}

/**
 * 系统精度
 *
 * @return
 */
private static int getScaleSystem() {
    return scaleSystem;
}

/**
 * 默认精度
 */
private static int scaleDefault = 8;
/**
 * 默认精度 系统超长精度
 */
private static int scaleDefaultMax = 16;

/**
 * 加
 * Add big decimal.
 *
 * @param v1 the v 1
 * @param v2 the v 2
 * @return the big decimal
 */
public static BigDecimal add(double v1, double v2) {
    BigDecimal b1 = new BigDecimal(Double.toString(v1));
    BigDecimal b2 = new BigDecimal(Double.toString(v2));
    return b1.add(b2);
}

public static BigDecimal add(String v1, String v2) {
    BigDecimal b1 = new BigDecimal(v1);
    BigDecimal b2 = new BigDecimal(v2);
    return b1.add(b2);
}

/**
 * 加
 * Add big decimal.
 *
 * @param b1 the v 1
 * @param b1 the v 2
 * @return the big decimal
 */
public static BigDecimal add(BigDecimal b1, BigDecimal b2) {
    return b1.add(b2);
}

/**
 * 减
 * Sub big decimal.
 *
 * @param v1 the v 1
 * @param v2 the v 2
 * @return the big decimal
 */
public static BigDecimal sub(double v1, double v2) {
    BigDecimal b1 = new BigDecimal(Double.toString(v1));
    BigDecimal b2 = new BigDecimal(Double.toString(v2));
    return b1.subtract(b2);
}

public static BigDecimal sub(String v1, String v2) {
    BigDecimal b1 = new BigDecimal(v1);
    BigDecimal b2 = new BigDecimal(v2);
    return b1.subtract(b2);
}

/**
 * 减
 * Sub big decimal.
 *
 * @param v1 the v 1
 * @param v2 the v 2
 * @return the big decimal
 */
public static BigDecimal sub(BigDecimal v1, BigDecimal v2) {
    return v1.subtract(v2);
}


/**
 * 乘
 * Mul big decimal.
 *
 * @param v1 the v 1
 * @param v2 the v 2
 * @return the big decimal
 */
public static BigDecimal mul(double v1, double v2) {
    BigDecimal b1 = new BigDecimal(Double.toString(v1));
    BigDecimal b2 = new BigDecimal(Double.toString(v2));
    return b1.multiply(b2);
}

public static BigDecimal mul(String v1, String v2) {
    BigDecimal b1 = new BigDecimal(v1);
    BigDecimal b2 = new BigDecimal(v2);
    return b1.multiply(b2);
}

/**
 * 乘
 * Mul big decimal.
 *
 * @param v1 the v 1
 * @param v2 the v 2
 * @return the big decimal
 */
public static BigDecimal mul(BigDecimal v1, BigDecimal v2) {
    return v1.multiply(v2);
}

/**
 * 除
 * Div big decimal.
 *
 * @param v1 the v 1
 * @param v2 the v 2
 * @return the big decimal
 */
public static BigDecimal div(double v1, double v2, int scale) {
    if (scale < 0) {
        throw new IllegalArgumentException("The scale must be a positive integer or zero");
    }
    BigDecimal b1 = new BigDecimal(Double.toString(v1));
    BigDecimal b2 = new BigDecimal(Double.toString(v2));
    return b1.divide(b2, scale, RoundingMode.HALF_UP);
}

public static BigDecimal div(String v1, String v2) {
    BigDecimal b1 = new BigDecimal(v1);
    BigDecimal b2 = new BigDecimal(v2);
    return b1.divide(b2);
}

/**
 * 除
 * Div big decimal.
 *
 * @param v1 the v 1
 * @param v2 the v 2
 * @return the big decimal
 */
public static BigDecimal div(double v1, double v2) {
    return div(v1, v2, scaleDefault);
}

/**
 * 除
 * Div big decimal.
 *
 * @param v1 the v 1
 * @param v2 the v 2
 * @return the big decimal
 */
public static BigDecimal div(BigDecimal v1, BigDecimal v2, int scale) {
    if (scale < 0) {
        throw new IllegalArgumentException("The scale must be a positive integer or zero");
    }
    return v1.divide(v2, scale, RoundingMode.HALF_UP);
}

/**
 * 除
 * Div big decimal.
 *
 * @param v1 the v 1
 * @param v2 the v 2
 * @return the big decimal
 */
public static BigDecimal divScale(BigDecimal v1, BigDecimal v2) {
    return div(v1, v2, getScaleSystem());
}

/**
 * 除
 * Div big decimal.
 *
 * @param v1 the v 1
 * @param v2 the v 2
 * @return the big decimal
 */
public static BigDecimal div(BigDecimal v1, BigDecimal v2) {
    return div(v1, v2, scaleDefault);
}

/**
 * 提供精确的小数位四舍五入处理。
 * Div big decimal.
 *
 * @param v1 the v 1
 * @return the big decimal
 */
public static BigDecimal round(double v1, int scale) {
    if (scale < 0) {
        throw new IllegalArgumentException("The scale must be a positive integer or zero");
    }
    BigDecimal b1 = new BigDecimal(Double.toString(v1));
    BigDecimal b2 = new BigDecimal("1");
    return b1.divide(b2, scale, RoundingMode.HALF_UP);
}

public static BigDecimal round(double v1) {
    return round(v1, scaleDefault);
}

public static BigDecimal roundScale(double v1) {
    return round(v1, getScaleSystem());
}

/**
 * 提供精确的小数位四舍五入处理。
 * Div big decimal.
 *
 * @param v1 the v 1
 * @return the big decimal
 */
public static BigDecimal round(BigDecimal v1, int scale) {
    if (scale < 0) {
        throw new IllegalArgumentException("The scale must be a positive integer or zero");
    }
    BigDecimal b2 = new BigDecimal("1");
    return v1.divide(b2, scale, RoundingMode.HALF_UP);
}

/**
 * 提供精确的小数位四舍五入处理。
 * Div big decimal.
 *
 * @param v1 the v 1
 * @return the big decimal
 */
public static BigDecimal round(BigDecimal v1) {
    return round(v1, scaleDefault);
}

/**
 * 提供精确的小数位四舍五入处理。
 * Div big decimal.
 *
 * @param v1 the v 1
 * @return the big decimal
 */
public static BigDecimal roundScale(BigDecimal v1) {
    return round(v1, getScaleSystem());
}

/**
 * 判断不为空,并且大于0
 *
 * @param a
 * @return
 */
public static boolean isNotNullAndGreaterThan0(BigDecimal a) {
    if (a == null) {
        return false;
    }
    return a.compareTo(BigDecimal.ZERO) > 0;
}

/**
 * 判断不为空
 *
 * @param a
 * @return
 */
public static boolean isNotNull(BigDecimal a) {
    if (a == null) {
        return false;
    }
    return true;
}

/**
 * 创建, 但是 值可以为 Null
 *
 * @param str
 * @return
 */
public static BigDecimal createBigDecimal(String str) {
    return NumberUtils.createBigDecimal(str);
}

/**
 * 赋值,自动判断 null,如果为null 时,自动返回 value值
 *
 * @param str
 * @param value 默认值
 * @return
 */
public static BigDecimal toBigDecimal(BigDecimal str, BigDecimal value) {
    if (!isNotNull(str)) {
        return value;
    }
    return str;
}

/**
 * 比较大小
 * 返回  -1  小 , 0 相等  ,1 大
 *
 * @param b1
 * @param b2
 * @return
 */
public static int compare(BigDecimal b1, BigDecimal b2) {
    return b1.compareTo(b2);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数
 *
 * @param str
 * @param scale
 * @return
 */
public static BigDecimal toBigDecimal(String str, int scale) {
    return NumberUtils.toScaledBigDecimal(str, scale, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal(String str) {
    return NumberUtils.toScaledBigDecimal(str, scaleDefault, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimalDefault(String str) {
    return NumberUtils.toScaledBigDecimal(str, scaleDefaultMax, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 指定 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimalScale(String str) {
    return NumberUtils.toScaledBigDecimal(str, getScaleSystem(), RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal2(String str) {
    return NumberUtils.toScaledBigDecimal(str, 2, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 4 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal4(String str) {
    return NumberUtils.toScaledBigDecimal(str, 4, RoundingMode.HALF_UP);
}

/**
 * BigDecimal 转换 为 BigDecimal ,保留小数位数
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal(BigDecimal str, int scale) {
    return NumberUtils.toScaledBigDecimal(str, scale, RoundingMode.HALF_UP);
}

/**
 * BigDecimal 转换 为 BigDecimal ,保留小数位数  2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal(BigDecimal str) {
    return NumberUtils.toScaledBigDecimal(str, scaleDefault, RoundingMode.HALF_UP);
}

/**
 * BigDecimal 转换 为 BigDecimal ,保留小数位数  2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimalDefault(BigDecimal str) {
    return NumberUtils.toScaledBigDecimal(str, scaleDefaultMax, RoundingMode.HALF_UP);
}

/**
 * BigDecimal 转换 为 BigDecimal ,保留小数位数  指定 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimalScale(BigDecimal str) {
    return NumberUtils.toScaledBigDecimal(str, getScaleSystem(), RoundingMode.HALF_UP);
}

/**
 * BigDecimal 转换 为 BigDecimal ,保留小数位数  2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal2(BigDecimal str) {
    return NumberUtils.toScaledBigDecimal(str, 2, RoundingMode.HALF_UP);
}

/**
 * BigDecimal 转换 为 BigDecimal ,保留小数位数  2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal4(BigDecimal str) {
    return NumberUtils.toScaledBigDecimal(str, 4, RoundingMode.HALF_UP);
}

/**
 * Double 转换 为 BigDecimal ,保留小数位数  2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal2(Double str) {
    return NumberUtils.toScaledBigDecimal(str, 2, RoundingMode.HALF_UP);
}

/**
 * Double 转换 为 BigDecimal ,保留小数位数  2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal(Double str) {
    return NumberUtils.toScaledBigDecimal(str, scaleDefault, RoundingMode.HALF_UP);
}

/**
 * Double 转换 为 BigDecimal ,保留小数位数  2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimalDefault(Double str) {
    return NumberUtils.toScaledBigDecimal(str, scaleDefaultMax, RoundingMode.HALF_UP);
}

/**
 * Double 转换 为 BigDecimal ,保留小数位数  指定 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimalScale(Double str) {
    return NumberUtils.toScaledBigDecimal(str, getScaleSystem(), RoundingMode.HALF_UP);
}

/**
 * Double 转换 为 BigDecimal ,保留小数位数  4 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal4(Double str) {
    return NumberUtils.toScaledBigDecimal(str, 4, RoundingMode.HALF_UP);
}

/**
 * Double 转换 为 BigDecimal ,保留小数位数
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal(Double str, int scale) {
    return NumberUtils.toScaledBigDecimal(str, scale, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal(Float str) {
    return NumberUtils.toScaledBigDecimal(str, scaleDefault, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimalDefault(Float str) {
    return NumberUtils.toScaledBigDecimal(str, scaleDefaultMax, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal2(Float str) {
    return NumberUtils.toScaledBigDecimal(str, 2, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 4 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal4(Float str) {
    return NumberUtils.toScaledBigDecimal(str, 4, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 指定 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal(Float str, int scale) {
    return NumberUtils.toScaledBigDecimal(str, scale, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 指定 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimalScale(Float str) {
    return NumberUtils.toScaledBigDecimal(str, getScaleSystem(), RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal(int str) {
    return NumberUtils.toScaledBigDecimal(String.valueOf(str), scaleDefault, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimalDefault(int str) {
    return NumberUtils.toScaledBigDecimal(String.valueOf(str), scaleDefaultMax, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal2(int str) {
    return NumberUtils.toScaledBigDecimal(String.valueOf(str), 2, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 4 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal4(int str) {
    return NumberUtils.toScaledBigDecimal(String.valueOf(str), 4, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 n 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal(int str, int scale) {
    return NumberUtils.toScaledBigDecimal(String.valueOf(str), scale, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 指定 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimalScale(int str) {
    return NumberUtils.toScaledBigDecimal(String.valueOf(str), getScaleSystem(), RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal(long str) {
    return NumberUtils.toScaledBigDecimal(String.valueOf(str), scaleDefault, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimalDefault(long str) {
    return NumberUtils.toScaledBigDecimal(String.valueOf(str), scaleDefaultMax, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal2(long str) {
    return NumberUtils.toScaledBigDecimal(String.valueOf(str), 2, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 4 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal4(long str) {
    return NumberUtils.toScaledBigDecimal(String.valueOf(str), 4, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 n 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal(long str, int scale) {
    return NumberUtils.toScaledBigDecimal(String.valueOf(str), scale, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 n 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimalScale(long str) {
    return NumberUtils.toScaledBigDecimal(String.valueOf(str), getScaleSystem(), RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal(float str) {
    return NumberUtils.toScaledBigDecimal(String.valueOf(str), scaleDefault, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimalDefault(float str) {
    return NumberUtils.toScaledBigDecimal(String.valueOf(str), scaleDefaultMax, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal2(float str) {
    return NumberUtils.toScaledBigDecimal(String.valueOf(str), 2, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 4 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal4(float str) {
    return NumberUtils.toScaledBigDecimal(String.valueOf(str), 4, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 n 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal(float str, int scale) {
    return NumberUtils.toScaledBigDecimal(String.valueOf(str), scale, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 n 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimalScale(float str) {
    return NumberUtils.toScaledBigDecimal(String.valueOf(str), getScaleSystem(), RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal(double str) {
    return NumberUtils.toScaledBigDecimal(String.valueOf(str), scaleDefault, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimalDefault(double str) {
    return NumberUtils.toScaledBigDecimal(String.valueOf(str), scaleDefaultMax, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 2 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal2(double str) {
    return NumberUtils.toScaledBigDecimal(String.valueOf(str), 2, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 4 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal4(double str) {
    return NumberUtils.toScaledBigDecimal(String.valueOf(str), 4, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 4 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimal(double str, int scale) {
    return NumberUtils.toScaledBigDecimal(String.valueOf(str), scale, RoundingMode.HALF_UP);
}

/**
 * 字符串转换 为 BigDecimal ,保留小数位数 4 位
 *
 * @param str
 * @return
 */
public static BigDecimal toBigDecimalScale(double str) {
    return NumberUtils.toScaledBigDecimal(String.valueOf(str), getScaleSystem(), RoundingMode.HALF_UP);
}

}

相关文章
|
28天前
|
Java
【Java】Math、System、RunTime、BigDecimal类常用方法
【Java】Math、System、RunTime、BigDecimal类常用方法
|
29天前
|
缓存 前端开发 Java
【前端学java】java基础巩固复习巩固语法练习-工具类的封装(14)
【8月更文挑战第10天】java基础巩固,工具类的封装
17 1
【前端学java】java基础巩固复习巩固语法练习-工具类的封装(14)
|
16天前
|
Java
Java应用结构规范问题之在UnitConvertUtils工具类将千米转换为米的问题如何解决
Java应用结构规范问题之在UnitConvertUtils工具类将千米转换为米的问题如何解决
|
1月前
|
安全 Java
12 Java常用类(二)(String类+时间类+BigDecimal类等等)
12 Java常用类(二)(String类+时间类+BigDecimal类等等)
25 2
|
28天前
|
存储 设计模式 安全
Java GenericObjectPool 对象池化技术--SpringBoot sftp 连接池工具类
Java GenericObjectPool 对象池化技术--SpringBoot sftp 连接池工具类
16 0
|
2月前
|
设计模式 存储 安全
Java面试题:设计一个线程安全的单例类并解释其内存占用情况?使用Java多线程工具类实现一个高效的线程池,并解释其背后的原理。结合观察者模式与Java并发框架,设计一个可扩展的事件处理系统
Java面试题:设计一个线程安全的单例类并解释其内存占用情况?使用Java多线程工具类实现一个高效的线程池,并解释其背后的原理。结合观察者模式与Java并发框架,设计一个可扩展的事件处理系统
43 1
|
2月前
|
安全 Java 开发者
Java中的并发工具类与线程安全实现
Java中的并发工具类与线程安全实现
|
3月前
|
设计模式 缓存 算法
编写高效的Java工具类:实用技巧与设计模式
编写高效的Java工具类:实用技巧与设计模式
|
2月前
|
并行计算 Java API
Java中的并发工具类详解
Java中的并发工具类详解
|
2月前
|
设计模式 缓存 算法
编写高效的Java工具类:实用技巧与设计模式
编写高效的Java工具类:实用技巧与设计模式