Java的BigDecimal,对运算封装
- 添加maven依赖
- <dependency>
- <groupId>com.google.guava</groupId>
- <artifactId>guava</artifactId>
- <version>18.0</version>
- </dependency>
import java.math.BigDecimal;
public class NumberArithmeticUtils {
/**
* BigDecimal的加法运算封装
* @author : shijing
* 2017年3月23日下午4:53:21
* @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加法运算的封装
* @author : shijing
* 2017年3月23日下午4:54:08
* @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;
}
/**
* 计算金额方法
* @author : shijing
* 2017年3月23日下午4:53:00
* @param b1
* @param bn
* @return
*/
public static BigDecimal safeSubtract(BigDecimal b1, BigDecimal... bn) {
return safeSubtract(true, b1, bn);
}
/**
* BigDecimal的安全减法运算
* @author : shijing
* 2017年3月23日下午4:50:45
* @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
* @author : shijing
* 2017年3月23日下午4:58:16
* @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 != r && r > 0 ? r : 0;
}
/**
* 金额除法计算,返回2位小数(具体的返回多少位大家自己看着改吧)
* @author : shijing
* 2017年3月23日下午5:02:17
* @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位,用于金额计算
* @author : shijing
* 2017年3月23日下午4:59:29
* @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的乘法运算封装
* @author : shijing
* 2017年3月23日下午5:01:57
* @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);
}
}
最新文章
- Java学习笔记11
- 解决java使用https协议请求出现证书不信任问题(PKIX path building failed)
- 服务器文件系统一定要用NTFS格式。
- Ninject 在 Winform、 Asp.net MVC中连络EntityFramework的应用
- ZOJ-3933-Team Formation【二分图最佳匹配】【KM】
- 两条命令,实现ssh免密登陆
- 借助表达式树感受不一样的CRUD
- 【待考察】Appium使用技巧,助你快速入门移动端自动化!
- Spring 自动定时任务配置
- VC++网络安全编程范例(11)-SSL高级加密网络通信(转)
- python协程的使用
- springBoot整合MongoDB(单机)
- POST方式";Content-type";是";application/x-www-form-urlencoded 的请求遇到的问题
- 使用CNN做数字识别和人脸识别
- kdissert:linux下的自由脑图软件
- MySQL 字段类型占用空间
- 5 Dockerfile指令详解 &;&; CMD 指令
- Unix系统编程()信号类型和默认行为
- centos7安装mysql5.7.18笔记
- HTML里面的文本标签