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. Hadoop框架:DataNode工作机制详解

    本文源码:GitHub·点这里 || GitEE·点这里 一.工作机制 1.基础描述 DataNode上数据块以文件形式存储在磁盘上,包括两个文件,一个是数据本身,一个是数据块元数据包括长度.校验.时 ...

  2. C# / VB.NET 在PPT中创建、编辑PPT SmartArt图形

    本文介绍通过C#和VB.NET程序代码来创建和编辑PPT文档中的SmartArt图形.文中将分两个操作示例来演示创建和编辑结果. 使用工具:Spire.Presentation for .NET ho ...

  3. C++学习笔记---数据类型

    1.整型 C++中能够表示整型的类型有几下几种方式,区别在于所占内存空间不足 数据类型 占用空间 取值范围 short(短整型) 2字节 (-2^15~2^15-1) int(整型) 4字节 (-2^ ...

  4. hdu3555 Bomb (数位dp入门题)

    Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submi ...

  5. node的function函数和路由代码的小例子

    1.node事件循环 事件: const events=require("events"); emt=new events.EventEmitter(); function eve ...

  6. Java8新特性探索之Stream接口

    一.为什么引入Stream流 流是一系列与特定存储机制无关的元素--实际上,流并没有"存储"之说.使用流,无需迭代集合中的元素,就可以从管道提取和操作元素.这些管道通常被组合在一起 ...

  7. 任务管理器中arcsom.exe和arcsoc.exe的个数问题

    转载链接:http://blog.newnaw.com/?p=78 安装了ArcGIS Server的机器,当打开任务管理器的时候,会看到里面有arcsom.exe和arcsoc.exe进程,但它们的 ...

  8. Luogu P3262 [JLOI2015]战争调度

    题意 给定一棵高度为 \(n\) 的完全二叉树,可以将节点设置成两种状态.如果某个叶子 \(x\) 的状态为 \(i\) 同时他的某个祖先也为 \(i\),那么这个叶子就会对祖先产生 \(f_{x,i ...

  9. 开发工具:Mybatis.Plus.插件三种方式的逆向工程

    本文源码:GitHub·点这里 || GitEE·点这里 一.逆向工程简介 在Java开发中,持久层最常用的框架就是mybatis,该框架需要编写sql语句,mybatis官方提供逆向工程,可以把数据 ...

  10. Charles使用part4——修改网络请求

    Charles提供了Map功能.Rewrite功能.Breakpoints功能,都可以达到修改服务器返回内容的目的,这三者的差异是: Map功能适合长期的将某些请求重定向到另一个网络地址或本地文件   ...