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. Anderson《空气动力学基础》5th读书笔记 第1记——流动相似性

    在飞机真正上天之前,我们常常需要制作出缩小版的模型放在风洞中吹呀吹,尽可能地模拟真实飞行中的参数,这时我们就需要实现流动相似性,这便是本记要讲的. 文章目录 一.流动相似性的标准 二.流动相似性的应用 ...

  2. Spring Boot入门之Hello World

    Spring Boot介绍 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不 ...

  3. deploy.php

    <?php namespace Deployer; require 'recipe/common.php'; // Project name set('application', 'tp_web ...

  4. Redis基础(二)数据库

    数据库 Redis服务器的所有数据库都保存在redisServer.db数组中,而数据库的数量则由redisServer.dbnum属性保存. struct redisServer { // .. / ...

  5. nb-iot技术实现跟踪功能的应用

    在互联网和连接的世界里,nb-iot风靡一时.企业和个人正在利用nb-iot技术和nb-iot设备的可靠,快速连接能力,对其技术系统进行渐进式更改,并创建一个互联的"智能"世界. ...

  6. linux 的mysql 主从备份

    1.原理 mysql主从配置的流程大体如图: 1)master会将变动记录到二进制日志里面: 2)master有一个I/O线程将二进制日志发送到slave; 3) slave有一个I/O线程把mast ...

  7. ZOJ 1006 Do the Untwish

    Do the Untwish 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1006 题意:给定密文按公式解密 注 ...

  8. Simulink中封装子系统

    学习目的: 使用simulink封装一个子系统,并将封装子系统放入到自定义的库中,可供建模时重复使用 功能:封装一个能够检测输入信号下降沿跳变的边沿检测模块,该模块可支持双击时修改内部参数.封装完成后 ...

  9. Redis基础—了解Redis是如何做数据持久化的

    之前的文章介绍了Redis的简单数据结构的相关使用和底层原理,这篇文章我们就来聊一下Redis应该如何保证高可用. 数据持久化 我们知道虽然单机的Redis虽然性能十分的出色, 单机能够扛住10w的Q ...

  10. 面试重灾区——JVM内存结构和GC

    JVM介绍 1. JVM的体系架构(内存模型) 绿色的为线程私有,橘色的为线程共有 2. 类加载器 负责将.class文件加载到内存中,并且将该文件中的数据结构转换为方法区中的数据结构,生成一个Cla ...