1. import org.slf4j.Logger;
    2. import org.slf4j.LoggerFactory;
    3. import java.math.BigDecimal;
    4. import java.text.DecimalFormat;
    5. public class NewMathUtil {
    6. private static final Logger logger = LoggerFactory.getLogger(NewMathUtil.class);
    7. /**
    8. * 自动解析字符串内容并计算,同时可设置保留小数点位数
    9. *
    10. * @param operatorStr 运算式,eg. 10/3 or 1*5 or 6+1 or 8-1
    11. * @param xsdws 保留小数点位数
    12. * @return
    13. */
    14. public static String autoCalc(String operatorStr, int xsdws) {
    15. char[] chars = "+-*/".toCharArray();
    16. for (char tempC : chars) {
    17. String operation = String.valueOf(tempC);
    18. if (operatorStr.contains(operation)) {
    19. return calcOperatorChar(operatorStr, xsdws);
    20. }
    21. }
    22. return null;
    23. }
    24. /**
    25. * 自动解析字符串内容并计算,同时可设置保留小数点位数
    26. *
    27. * @param operatorStr 运算式,eg. 10/3 or 1*5 or 6+1 or 8-1
    28. * @param xsdws 保留小数点位数
    29. * @param bigDecimalRule 小数点的取舍规则
    30. * @return
    31. */
    32. public static String autoCalc(String operatorStr, int xsdws, Integer bigDecimalRule) {
    33. char[] chars = "+-*/".toCharArray();
    34. for (char tempC : chars) {
    35. String operation = String.valueOf(tempC);
    36. if (operatorStr.contains(operation)) {
    37. return calcOperatorChar(operatorStr, xsdws, bigDecimalRule);
    38. }
    39. }
    40. return null;
    41. }
    42. /**
    43. * 整数只保留整数,小数统一保留2位小数。
    44. * eg. "100", "100.0", "100.00", "0", "0.0", "0.00", "0.1", "0.12", "3", "3.0", "3.1", "3.12", "3.345"
    45. * --> 100, 100, 100, 0, 0, 0, 0.10, 0.12, 3, 3, 3.10, 3.12, 3.34
    46. *
    47. * @param rate
    48. * @return
    49. */
    50. public static String keep2DecimalPlacesExceptInteger(String rate) {
    51. if (rate != null) {
    52. if (rate.startsWith("100")) {// 100 or 100.0 100.00 --> 100
    53. rate = "100";
    54. } else if (rate.equals("0") || rate.equals("0.0") || rate.equals("0.00")) {// 0 or 0.0 or 0.00 --> 0
    55. rate = "0";
    56. } else if (rate.endsWith(".0") || rate.endsWith(".00")) {// 3.0 or 3.00 --> 3
    57. rate = rate.split("\\.")[0];
    58. } else if (rate.contains(".")) {
    59. if (rate.split("\\.")[1].length() == 1) {// 3.1 --> 3.10
    60. rate += "0";
    61. } else if (rate.split("\\.")[1].length() > 1) {// 3.345 --> 3.34
    62. rate = rate.split("\\.")[0] + "." + rate.split("\\.")[1].substring(0, 2);
    63. }
    64. } /*else if (!rate.contains(".")) {// 10 --> 10
    65. //
    66. }*/
    67. }
    68. return rate;
    69. }
    70. /**
    71. * 功能:输入字符串的计算式得到指定位数的结果
    72. *
    73. * @param operator 计算式(如 10/3)
    74. * @param ws 四舍五入,保留几位小数(默认保留2位小数)
    75. * @param <T>
    76. * @return
    77. */
    78. public static <T> String calcOperatorChar(String operator, T ws) {
    79. return method(operator, ws, BigDecimal.ROUND_HALF_UP);
    80. }
    81. public static <T> String calcOperatorChar(String operator, T xsdws, Integer bigDecimalRule) {
    82. if (bigDecimalRule == null) {
    83. // 小数点保留规则:默认"四舍五入"
    84. bigDecimalRule = BigDecimal.ROUND_HALF_UP;
    85. }
    86. // ROUND_HALF_UP:四舍五入
    87. // ROUND_HALF_DOWN:五舍六入
    88. // ROUND_DOWN:直接删除多余的小数位
    89. // ROUND_UP:进位处理
    90. return method(operator, xsdws, bigDecimalRule);
    91. }
    92. private static <T> String method(String operator, T xsdNum, Integer roundRule) {
    93. int xsdws = 0;
    94. if (xsdNum == null) {// 默认保留2位小数
    95. xsdws = 2;
    96. } else if (xsdNum instanceof Integer) {
    97. xsdws = (Integer) xsdNum;
    98. } else if (xsdNum instanceof String) {
    99. xsdws = Integer.parseInt((String) xsdNum);
    100. } else {
    101. throw new RuntimeException("ws 格式有误(只能为 String or Integer 类型)");
    102. }
    103. operator = operator.replaceAll(" ", "");
    104. String[] split = operator.split("[+,-,*,/]");
    105. if (split.length != 2 || (split.length == 2 && operator.length() - split[0].length() - split[1].length() != 1)) {
    106. throw new RuntimeException("运算符非 +-*/ 中的一种 or 存在多个运算符 or 运算式有误");
    107. }
    108. String s1 = split[0];
    109. String s2 = split[1];
    110. BigDecimal bigDecimal = null;
    111. if (operator.contains("+")) {
    112. bigDecimal = new BigDecimal(s1).add(new BigDecimal(s2));
    113. } else if (operator.contains("-")) {
    114. bigDecimal = new BigDecimal(s1).subtract(new BigDecimal(s2));
    115. } else if (operator.contains("*")) {
    116. bigDecimal = new BigDecimal(s1).multiply(new BigDecimal(s2));
    117. } else if (operator.contains("/")) {
    118. bigDecimal = new BigDecimal(s1).divide(new BigDecimal(s2), xsdws, roundRule != null ? roundRule : BigDecimal.ROUND_HALF_UP);
    119. } else {
    120. throw new RuntimeException("operator not contains +-*/");
    121. }
    122. return keepXSD(xsdws, bigDecimal);
    123. }
    124. private static <T> String keepXSD(int xsdCount, T num) {
    125. StringBuilder zwf = new StringBuilder().append("#0.");// 占位符
    126. for (int i = 0; i < xsdCount; i++) {
    127. zwf.append("0");
    128. }
    129. BigDecimal bigDecimal = null;
    130. if (num instanceof String) {
    131. bigDecimal = new BigDecimal((String) num);
    132. } else if (num instanceof BigDecimal) {
    133. bigDecimal = (BigDecimal) num;
    134. } else if (num instanceof Long) {
    135. bigDecimal = new BigDecimal((Long) num);
    136. } else if (num instanceof Float) {
    137. bigDecimal = new BigDecimal((Float) num);
    138. } else if (num instanceof Double) {
    139. bigDecimal = new BigDecimal((Double) num);
    140. } else {
    141. throw new RuntimeException("num 只支持 int、long、double、float、String 几种类型");
    142. }
    143. String format = new DecimalFormat(zwf.toString()).format(bigDecimal);
    144. // .123 ==> 0.123
    145. if (format.startsWith(".")) {
    146. format = "0" + format;
    147. }
    148. // 10. ==> 10
    149. if (format.endsWith(".")) {
    150. format = format.substring(0, format.length() - 1);
    151. }
    152. return format;
    153. }
    154. }