1. 添加maven依赖
  2. <dependency>
  3. <groupId>com.google.guava</groupId>
  4. <artifactId>guava</artifactId>
  5. <version>18.0</version>
  6. </dependency>
  7. import java.math.BigDecimal;

  8. public class NumberArithmeticUtils {

  9. /**

  10. * BigDecimal的加法运算封装

  11. * @author : shijing

  12. * 2017年3月23日下午4:53:21

  13. * @param b1

  14. * @param bn

  15. * @return

  16. */

  17. public static BigDecimal safeAdd(BigDecimal b1, BigDecimal... bn) {

  18. if (null == b1) {

  19. b1 = BigDecimal.ZERO;

  20. }

  21. if (null != bn) {

  22. for (BigDecimal b : bn) {

  23. b1 = b1.add(null == b ? BigDecimal.ZERO : b);

  24. }

  25. }

  26. return b1;

  27. }

  28. /**

  29. * Integer加法运算的封装

  30. * @author : shijing

  31. * 2017年3月23日下午4:54:08

  32. * @param b1   第一个数

  33. * @param bn   需要加的加法数组

  34. * @注 : Optional  是属于com.google.common.base.Optional<T> 下面的class

  35. * @return

  36. */

  37. public static Integer safeAdd(Integer b1, Integer... bn) {

  38. if (null == b1) {

  39. b1 = 0;

  40. }

  41. Integer r = b1;

  42. if (null != bn) {

  43. for (Integer b : bn) {

  44. r += Optional.fromNullable(b).or(0);

  45. }

  46. }

  47. return r > 0 ? r : 0;

  48. }

  49. /**

  50. * 计算金额方法

  51. * @author : shijing

  52. * 2017年3月23日下午4:53:00

  53. * @param b1

  54. * @param bn

  55. * @return

  56. */

  57. public static BigDecimal safeSubtract(BigDecimal b1, BigDecimal... bn) {

  58. return safeSubtract(true, b1, bn);

  59. }

  60. /**

  61. * BigDecimal的安全减法运算

  62. * @author : shijing

  63. * 2017年3月23日下午4:50:45

  64. * @param isZero  减法结果为负数时是否返回0,true是返回0(金额计算时使用),false是返回负数结果

  65. * @param b1        被减数

  66. * @param bn        需要减的减数数组

  67. * @return

  68. */

  69. public static BigDecimal safeSubtract(Boolean isZero, BigDecimal b1, BigDecimal... bn) {

  70. if (null == b1) {

  71. b1 = BigDecimal.ZERO;

  72. }

  73. BigDecimal r = b1;

  74. if (null != bn) {

  75. for (BigDecimal b : bn) {

  76. r = r.subtract((null == b ? BigDecimal.ZERO : b));

  77. }

  78. }

  79. return isZero ? (r.compareTo(BigDecimal.ZERO) == -1 ? BigDecimal.ZERO : r) : r;

  80. }

  81. /**

  82. * 整型的减法运算,小于0时返回0

  83. * @author : shijing

  84. * 2017年3月23日下午4:58:16

  85. * @param b1

  86. * @param bn

  87. * @return

  88. */

  89. public static Integer safeSubtract(Integer b1, Integer... bn) {

  90. if (null == b1) {

  91. b1 = 0;

  92. }

  93. Integer r = b1;

  94. if (null != bn) {

  95. for (Integer b : bn) {

  96. r -= Optional.fromNullable(b).or(0);

  97. }

  98. }

  99. return  != r && r > 0 ? r : 0;

  100. }

  101. /**

  102. * 金额除法计算,返回2位小数(具体的返回多少位大家自己看着改吧)

  103. * @author : shijing

  104. * 2017年3月23日下午5:02:17

  105. * @param b1

  106. * @param b2

  107. * @return

  108. */

  109. public static <T extends Number> BigDecimal safeDivide(T b1, T b2){

  110. return safeDivide(b1, b2, BigDecimal.ZERO);

  111. }

  112. /**

  113. * BigDecimal的除法运算封装,如果除数或者被除数为0,返回默认值

  114. * 默认返回小数位后2位,用于金额计算

  115. * @author : shijing

  116. * 2017年3月23日下午4:59:29

  117. * @param b1

  118. * @param b2

  119. * @param defaultValue

  120. * @return

  121. */

  122. public static <T extends Number> BigDecimal safeDivide(T b1, T b2, BigDecimal defaultValue) {

  123. if (null == b1 ||  null == b2) {

  124. return defaultValue;

  125. }

  126. try {

  127. return BigDecimal.valueOf(b1.doubleValue()).divide(BigDecimal.valueOf(b2.doubleValue()), 2, BigDecimal.ROUND_HALF_UP);

  128. } catch (Exception e) {

  129. return defaultValue;

  130. }

  131. }

  132. /**

  133. * BigDecimal的乘法运算封装

  134. * @author : shijing

  135. * 2017年3月23日下午5:01:57

  136. * @param b1

  137. * @param b2

  138. * @return

  139. */

  140. public static <T extends Number> BigDecimal safeMultiply(T b1, T b2) {

  141. if (null == b1 ||  null == b2) {

  142. return BigDecimal.ZERO;

  143. }

  144. return BigDecimal.valueOf(b1.doubleValue()).multiply(BigDecimal.valueOf(b2.doubleValue())).setScale(2, BigDecimal.ROUND_HALF_UP);

  145. }

  146. }

Java的BigDecimal,对运算封装的更多相关文章

  1. Java工具类之——BigDecimal运算封装(包含金额的计算方式)

    日常对于金额计算,应该都是用的BigDecimal,  可是苦于没有好的工具类方法,现在贡献一个我正在用的对于数字计算的工具类,项目中就是用的这个,简单粗暴好用,话不多说,代码奉上(该工具类需要引入g ...

  2. Java中的Bigdecimal类型运算

    Java中的Bigdecimal类型运算 双精度浮点型变量double可以处理16位有效数.在实际应用中,需要对更大或者更小的数进行运算和处理.Java在java.math包中提 供的API类BigD ...

  3. BigDecimal除法运算出现java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result的解决办法

    BigDecimal除法运算出现java.lang.ArithmeticException: Non-terminating decimal expansion; no exact represent ...

  4. Java中的数学运算BigDecimal

    Math类 package ch7; /** * Created by Jiqing on 2016/11/24. */ public class MathDemo { public static v ...

  5. Java使用BigDecimal保留double、float运算精度、保留指定位数有效数字、四舍五入

    工具类 package --; import java.math.BigDecimal; /** * Created by kongqw on 2015/12/10. */ public final ...

  6. 解决java.math.BigDecimal divide方法运算结果为无限小数问题

    http://samueli.iteye.com/blog/224755 BigDecimal除法运算报错,错误如下:Non-terminating decimal expansion; no exa ...

  7. Java使用BigDecimal解决浮点型运算丢失精度的问题

    @Test public void test1(){ System.out.print(0.05+0.01); } @Test public void test2(){ BigDecimal b1 = ...

  8. java中BigDecimal加减乘除基本用法

    Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算.双精度浮点型变量double可以处理16位有效数. 在实际应用中,需要对更大或者更小的数进 ...

  9. Java中BigDecimal的8种舍入模式

    java.math.BigDecimal 不可变的.任意精度的有符号十进制数.BigDecimal 由任意精度的整数非标度值和32位的整数标度(scale)组成. 如果为零或正数,则标度是小数点后的位 ...

随机推荐

  1. 建议你吃透python这68个内置函数!

    内置函数就是Python给你提供的, 拿来直接用的函数,比如print,input等. 截止到python版本3.6.2 ,一共提供了68个内置函数,具体如下 abs() dict() help() ...

  2. JMeter性能测试工具使用入门

    目录 安装 下载安装包 解压 添加到环境变量 启动 切换中文 打开日志查看 使用示例 创建线程组 添加HTTP接口 添加察看结果树 运行测试 添加断言 添加自定义变量 JMeter是一款强大的性能测试 ...

  3. 【踩坑系列】使用long类型处理金额,科学计数法导致金额转大写异常

    1. 踩坑经历 上周,一个用户反馈他创建的某个销售单无法打开,但其余销售单都可以正常打开,当时查看了生产环境的ERROR日志,发现抛了这样的异常:java.lang.NumberFormatExcep ...

  4. Sword Art Online 刀剑神域

    date: 2014-10-06 15:30:11 updated: 2014-10-06 15:30:11 [一] 他和她,第一次相见是在游戏里,两个角色的对话.现在说来都不算是正式见面呢. &qu ...

  5. Jenkins 凭证管理 - 看这一篇就够了~

    目录 Credential 类型 Credential 安全 Credential 创建 Credential ID 定义 Credential 使用 Credential 相关插件 最佳实践 许多三 ...

  6. Redis---08Redis集群(一)

    一.什么是Redis集群 Redis 集群实现了对Redis的水平扩容,即启动N个redis节点,将整个数据库分布存储在这N个节点中,每个节点存储总数据的1/N. Redis 集群通过分区(parti ...

  7. 编写shell脚本的规范

    目录 编写shell脚本的一些规范 解释器 添加脚本版本和注释功能 添加脚本调试 变量命名 全局变量和局部变量 命名规范 函数命名 脚本命名 函数 引用模块或文件 脚本日志 配置文件 其他 编写she ...

  8. android 下的 handler Message

    研究了下android下的 handler  message 实现原理: new handler() 的时候  从ThreadLocal里面 获取当前线程下的 Looper实例下的 MessageQu ...

  9. go-zero 是如何追踪你的请求链路的

    go-zero 是如何追踪你的请求链路 微服务架构中,调用链可能很漫长,从 http 到 rpc ,又从 rpc 到 http .而开发者想了解每个环节的调用情况及性能,最佳方案就是 全链路跟踪. 追 ...

  10. python机器学习实现人脸图片自动补全

    人脸自动补全 关注公众号"轻松学编程"了解更多. 1.导包 import matplotlib.pyplot as plt import numpy as np import pa ...