软件编程
位置:首页>> 软件编程>> java编程>> Java使用BigDecimal进行运算封装的实际案例

Java使用BigDecimal进行运算封装的实际案例

作者:执笔记忆的空白  发布时间:2023-06-20 02:22:26 

标签:java,bigdecimal,工具类,运算,封装

日常对于金额计算,应该都是用的BigDecimal,可是苦于没有好的工具类方法,现在贡献一个我正在用的对于数字计算的工具类,项目中就是用的这个,简单粗暴好用,话不多说,代码奉上(该工具类需要引入google的一个jar,com.google.common.base.Optional,具体maven引入看文章末尾):


import java.math.BigDecimal;
public class NumberArithmeticUtils {
/**
* BigDecimal的加法运算封装
* @param b1
* @param bn
* @return
*/
public static BigDecimal safeAdd(BigDecimal b1, BigDecimal... bn) {
if (null == b1) {
 b1 = BigDecimal.ZERO;
}
if (null != bn) {
 for (BigDecimal b : bn) {
 b1 = b1.add(null == b ? BigDecimal.ZERO : b);
 }
}
return b1;
}
/**
* Integer加法运算的封装
* @param b1 第一个数
* @param bn 需要加的加法数组
* @注 : Optional 是属于com.google.common.base.Optional<T> 下面的class
* @return
*/
public static Integer safeAdd(Integer b1, Integer... bn) {
if (null == b1) {
 b1 = 0;
}
Integer r = b1;
if (null != bn) {
 for (Integer b : bn) {
 r += Optional.fromNullable(b).or(0);
 }
}
return r > 0 ? r : 0;
}
/**
* 计算金额方法
* @param b1
* @param bn
* @return
*/
public static BigDecimal safeSubtract(BigDecimal b1, BigDecimal... bn) {
return safeSubtract(true, b1, bn);
}
/**
* BigDecimal的安全减法运算
* @param isZero 减法结果为负数时是否返回0,true是返回0(金额计算时使用),false是返回负数结果
* @param b1 被减数
* @param bn 需要减的减数数组
* @return
*/
public static BigDecimal safeSubtract(Boolean isZero, BigDecimal b1, BigDecimal... bn) {
if (null == b1) {
 b1 = BigDecimal.ZERO;
}
BigDecimal r = b1;
if (null != bn) {
 for (BigDecimal b : bn) {
 r = r.subtract((null == b ? BigDecimal.ZERO : b));
 }
}
return isZero ? (r.compareTo(BigDecimal.ZERO) == -1 ? BigDecimal.ZERO : r) : r;
}
/**
* 整型的减法运算,小于0时返回0
* @param b1
* @param bn
* @return
*/
public static Integer safeSubtract(Integer b1, Integer... bn) {
if (null == b1) {
 b1 = 0;
}
Integer r = b1;
if (null != bn) {
 for (Integer b : bn) {
 r -= Optional.fromNullable(b).or(0);
 }
}
return null != r && r > 0 ? r : 0;
}
/**
* 金额除法计算,返回2位小数(具体的返回多少位大家自己看着改吧)
* @param b1
* @param b2
* @return
*/
public static <T extends Number> BigDecimal safeDivide(T b1, T b2){
return safeDivide(b1, b2, BigDecimal.ZERO);
}
/**
* BigDecimal的除法运算封装,如果除数或者被除数为0,返回默认值
* 默认返回小数位后2位,用于金额计算
* @param b1
* @param b2
* @param defaultValue
* @return
*/
public static <T extends Number> BigDecimal safeDivide(T b1, T b2, BigDecimal defaultValue) {
if (null == b1 || null == b2) {
 return defaultValue;
}
try {
 return BigDecimal.valueOf(b1.doubleValue()).divide(BigDecimal.valueOf(b2.doubleValue()), 2, BigDecimal.ROUND_HALF_UP);
} catch (Exception e) {
 return defaultValue;
}
}
/**
* BigDecimal的乘法运算封装
* @param b1
* @param b2
* @return
*/
public static <T extends Number> BigDecimal safeMultiply(T b1, T b2) {
if (null == b1 || null == b2) {
 return BigDecimal.ZERO;
}
return BigDecimal.valueOf(b1.doubleValue()).multiply(BigDecimal.valueOf(b2.doubleValue())).setScale(2, BigDecimal.ROUND_HALF_UP);
}
}

Optional所在的jar以及版本:guava-18.0.ar

pom.xml配置:


<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>

来源:https://blog.csdn.net/moneyshi/article/details/65445820

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com