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的BigDecimal,对运算封装的更多相关文章
- Java工具类之——BigDecimal运算封装(包含金额的计算方式)
日常对于金额计算,应该都是用的BigDecimal, 可是苦于没有好的工具类方法,现在贡献一个我正在用的对于数字计算的工具类,项目中就是用的这个,简单粗暴好用,话不多说,代码奉上(该工具类需要引入g ...
- Java中的Bigdecimal类型运算
Java中的Bigdecimal类型运算 双精度浮点型变量double可以处理16位有效数.在实际应用中,需要对更大或者更小的数进行运算和处理.Java在java.math包中提 供的API类BigD ...
- BigDecimal除法运算出现java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result的解决办法
BigDecimal除法运算出现java.lang.ArithmeticException: Non-terminating decimal expansion; no exact represent ...
- Java中的数学运算BigDecimal
Math类 package ch7; /** * Created by Jiqing on 2016/11/24. */ public class MathDemo { public static v ...
- Java使用BigDecimal保留double、float运算精度、保留指定位数有效数字、四舍五入
工具类 package --; import java.math.BigDecimal; /** * Created by kongqw on 2015/12/10. */ public final ...
- 解决java.math.BigDecimal divide方法运算结果为无限小数问题
http://samueli.iteye.com/blog/224755 BigDecimal除法运算报错,错误如下:Non-terminating decimal expansion; no exa ...
- Java使用BigDecimal解决浮点型运算丢失精度的问题
@Test public void test1(){ System.out.print(0.05+0.01); } @Test public void test2(){ BigDecimal b1 = ...
- java中BigDecimal加减乘除基本用法
Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算.双精度浮点型变量double可以处理16位有效数. 在实际应用中,需要对更大或者更小的数进 ...
- Java中BigDecimal的8种舍入模式
java.math.BigDecimal 不可变的.任意精度的有符号十进制数.BigDecimal 由任意精度的整数非标度值和32位的整数标度(scale)组成. 如果为零或正数,则标度是小数点后的位 ...
随机推荐
- 建议你吃透python这68个内置函数!
内置函数就是Python给你提供的, 拿来直接用的函数,比如print,input等. 截止到python版本3.6.2 ,一共提供了68个内置函数,具体如下 abs() dict() help() ...
- JMeter性能测试工具使用入门
目录 安装 下载安装包 解压 添加到环境变量 启动 切换中文 打开日志查看 使用示例 创建线程组 添加HTTP接口 添加察看结果树 运行测试 添加断言 添加自定义变量 JMeter是一款强大的性能测试 ...
- 【踩坑系列】使用long类型处理金额,科学计数法导致金额转大写异常
1. 踩坑经历 上周,一个用户反馈他创建的某个销售单无法打开,但其余销售单都可以正常打开,当时查看了生产环境的ERROR日志,发现抛了这样的异常:java.lang.NumberFormatExcep ...
- Sword Art Online 刀剑神域
date: 2014-10-06 15:30:11 updated: 2014-10-06 15:30:11 [一] 他和她,第一次相见是在游戏里,两个角色的对话.现在说来都不算是正式见面呢. &qu ...
- Jenkins 凭证管理 - 看这一篇就够了~
目录 Credential 类型 Credential 安全 Credential 创建 Credential ID 定义 Credential 使用 Credential 相关插件 最佳实践 许多三 ...
- Redis---08Redis集群(一)
一.什么是Redis集群 Redis 集群实现了对Redis的水平扩容,即启动N个redis节点,将整个数据库分布存储在这N个节点中,每个节点存储总数据的1/N. Redis 集群通过分区(parti ...
- 编写shell脚本的规范
目录 编写shell脚本的一些规范 解释器 添加脚本版本和注释功能 添加脚本调试 变量命名 全局变量和局部变量 命名规范 函数命名 脚本命名 函数 引用模块或文件 脚本日志 配置文件 其他 编写she ...
- android 下的 handler Message
研究了下android下的 handler message 实现原理: new handler() 的时候 从ThreadLocal里面 获取当前线程下的 Looper实例下的 MessageQu ...
- go-zero 是如何追踪你的请求链路的
go-zero 是如何追踪你的请求链路 微服务架构中,调用链可能很漫长,从 http 到 rpc ,又从 rpc 到 http .而开发者想了解每个环节的调用情况及性能,最佳方案就是 全链路跟踪. 追 ...
- python机器学习实现人脸图片自动补全
人脸自动补全 关注公众号"轻松学编程"了解更多. 1.导包 import matplotlib.pyplot as plt import numpy as np import pa ...