code: private static BigDecimal doCalculate(String formula) throws Exception {
          Stack<BigDecimal> stack = new Stack<>();
          BigDecimal result = BigDecimal.valueOf(0);
          BigDecimal number = BigDecimal.valueOf(0);
          char sign = '+';
          boolean isDecimal = false;
          BigDecimal decimalSign = new BigDecimal(0.1);
  
          for (int i = 0; i < formula.length(); i++) {
              char c = formula.charAt(i);
              if (Character.isDigit(c)) {
                  BigDecimal num = new BigDecimal(c - '0');
                  number = isDecimal ? number.add(num.multiply(decimalSign)) : num.add(number.multiply(BigDecimal
                          .valueOf(10)));
                  decimalSign = decimalSign.multiply(BigDecimal.valueOf(0.1));
              } else if (c == '.') {
                  isDecimal = true;
                  decimalSign = BigDecimal.valueOf(0.1);
              } else if (c == '(') {
                  BigDecimal[] res = calculateInlineFormula(i + 1, formula);
                  number = number.compareTo(BigDecimal.valueOf(0)) == 0 ? res[0] : number.multiply(res[0]);
                  i = res[1].intValue();
              }
              boolean isNotDigit = (!Character.isDigit(c) && c != '.' && c != '(') || i == formula.length() - 1;
              if (isNotDigit) {
                  if (sign == '+') {
                      stack.push(number);
                  } else if (sign == '-') {
                      stack.push(number.multiply(BigDecimal.valueOf(-1)));
                  } else if (sign == '*') {
                      stack.push(number.multiply(stack.pop()));
                  } else if (sign == '/') {
                      stack.push(stack.pop().divide(number, getPrecision(), BigDecimal.ROUND_HALF_UP));
                  }
                  sign = c;
                  isDecimal = false;
                  number = BigDecimal.valueOf(0);
              }
          }
          for (BigDecimal i : stack) {
              result = result.add(i);
          }
          return result.setScale(getPrecision(), BigDecimal.ROUND_HALF_UP);
      }
docs: 开始计算
  
  @param formula 表达式
  
  @return {@link BigDecimal}
  
  @throws Exception 异常