NumberUtils.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package com.jiayue.pfr.util;
  2. import lombok.extern.slf4j.Slf4j;
  3. import java.math.BigDecimal;
  4. /**
  5. * 提供高精度的运算支持
  6. *
  7. * @author bizy
  8. * @version 2.0
  9. */
  10. @Slf4j
  11. public class NumberUtils {
  12. private NumberUtils() {
  13. }
  14. /**
  15. * 精确的加法运算.
  16. *
  17. * @param b1 加数
  18. * @param b2 加数
  19. * @param scale 运算结果小数后精确的位数
  20. * @return 结果
  21. */
  22. public static BigDecimal add(BigDecimal b1, BigDecimal b2, int scale) {
  23. return b1.add(b2).setScale(scale, BigDecimal.ROUND_HALF_UP);
  24. }
  25. /**
  26. * 精确的加法运算.
  27. *
  28. * @param b1 加数
  29. * @param b2 加数
  30. * @return 保留2位小数
  31. */
  32. public static BigDecimal add(BigDecimal b1, BigDecimal b2) {
  33. return b1.add(b2).setScale(2, BigDecimal.ROUND_HALF_UP);
  34. }
  35. /**
  36. * 精确的减法运算.
  37. *
  38. * @param b1 被减数
  39. * @param b2 减数
  40. * @param scale 运算结果小数后精确的位数
  41. * @return 结果
  42. */
  43. public static BigDecimal subtract(BigDecimal b1, BigDecimal b2, int scale) {
  44. return b1.subtract(b2).setScale(scale, BigDecimal.ROUND_HALF_UP);
  45. }
  46. /**
  47. * 精确的减法运算.
  48. *
  49. * @param b1 被减数
  50. * @param b2 减数
  51. * @return 结果 保留2位小数
  52. */
  53. public static BigDecimal subtract(BigDecimal b1, BigDecimal b2) {
  54. return b1.subtract(b2).setScale(2, BigDecimal.ROUND_HALF_UP);
  55. }
  56. /**
  57. * 提供精确的乘法运算,并对运算结果截位.
  58. *
  59. * @param b1 乘数
  60. * @param b2 乘数
  61. * @param scale 运算结果小数后精确的位数
  62. * @return 结果
  63. */
  64. public static BigDecimal multiply(BigDecimal b1, BigDecimal b2, int scale) {
  65. if (scale < 0) {
  66. throw new IllegalArgumentException("The scale must be a positive integer or zero");
  67. }
  68. return b1.multiply(b2).setScale(scale, BigDecimal.ROUND_HALF_UP);
  69. }
  70. /**
  71. * 提供精确的乘法运算,并对运算结果截位.
  72. *
  73. * @param b1 乘数
  74. * @param b2 乘数
  75. * @return 结果 保留2位小数
  76. */
  77. public static BigDecimal multiply(BigDecimal b1, BigDecimal b2) {
  78. return b1.multiply(b2).setScale(2, BigDecimal.ROUND_HALF_UP);
  79. }
  80. /**
  81. * 提供(相对)精确的除法运算. 由scale参数指定精度,以后的数字四舍五入.
  82. *
  83. * @param b1 被除数
  84. * @param b2 除数
  85. * @param scale 表示表示需要精确到小数点以后几位
  86. * @return 结果
  87. */
  88. public static BigDecimal divide(BigDecimal b1, BigDecimal b2, int scale) {
  89. if (scale < 0) {
  90. throw new IllegalArgumentException("The scale must be a positive integer or zero");
  91. }
  92. return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP);
  93. }
  94. /**
  95. * 提供(相对)精确的除法运算. 由scale参数指定精度,以后的数字四舍五入.
  96. *
  97. * @param b1 被除数
  98. * @param b2 除数
  99. * @return 结果 保留2位小数
  100. */
  101. public static BigDecimal divide(BigDecimal b1, BigDecimal b2) {
  102. return b1.divide(b2, 2, BigDecimal.ROUND_HALF_UP);
  103. }
  104. /**
  105. * 提供精确的小数位四舍五入处理.
  106. *
  107. * @param b 需要四舍五入的数字
  108. * @param scale 小数点后保留几位
  109. * @return TODO
  110. */
  111. public static BigDecimal round(BigDecimal b, int scale) {
  112. if (scale < 0) {
  113. throw new IllegalArgumentException("The scale must be a positive integer or zero");
  114. }
  115. return b.setScale(scale, BigDecimal.ROUND_HALF_UP);
  116. }
  117. /**
  118. * 提供精确的小数位四舍五入处理.
  119. *
  120. * @param b 需要四舍五入的数字
  121. * @return 保留2位小数
  122. */
  123. public static BigDecimal round(BigDecimal b) {
  124. return b.setScale(2, BigDecimal.ROUND_HALF_UP);
  125. }
  126. }