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. }

最新文章

  1. Java学习笔记11
  2. 解决java使用https协议请求出现证书不信任问题(PKIX path building failed)
  3. 服务器文件系统一定要用NTFS格式。
  4. Ninject 在 Winform、 Asp.net MVC中连络EntityFramework的应用
  5. ZOJ-3933-Team Formation【二分图最佳匹配】【KM】
  6. 两条命令,实现ssh免密登陆
  7. 借助表达式树感受不一样的CRUD
  8. 【待考察】Appium使用技巧,助你快速入门移动端自动化!
  9. Spring 自动定时任务配置
  10. VC++网络安全编程范例(11)-SSL高级加密网络通信(转)
  11. python协程的使用
  12. springBoot整合MongoDB(单机)
  13. POST方式&quot;Content-type&quot;是&quot;application/x-www-form-urlencoded 的请求遇到的问题
  14. 使用CNN做数字识别和人脸识别
  15. kdissert:linux下的自由脑图软件
  16. MySQL 字段类型占用空间
  17. 5 Dockerfile指令详解 &amp;&amp; CMD 指令
  18. Unix系统编程()信号类型和默认行为
  19. centos7安装mysql5.7.18笔记
  20. HTML里面的文本标签

热门文章

  1. SpringMVC异常的处理机制
  2. JAVA 基于Jusup爬虫
  3. node运行js获得输出的三种方式
  4. springboot添加拦截器
  5. Django( 学习第四部 Django的views视)
  6. Stream(四)
  7. 用 Span 对 C# 进程中三大内存区域进行统一访问 ,太厉害了!
  8. Moment.js常见用法总结
  9. codefroces中的病毒,这题有很深的trick,你能解开吗?
  10. Redis---00概述