Recently, I wrote a lot of formulas for automatic test case design. I felt that it was not impossible to write calculation formulas, but it was troublesome and not necessarily correct. I started to write some stupid methods to deal with it, and suddenly I realized that there must be related packages that have solved similar methods.

  1. Introduction of package

    Mons < dependency > < groupId > org.apache.com < / groupId > < artifactId > Commons - jexl3 < / artifactId > < version > 3.1 < / version > </dependency>Copy the code
  2. code

    Format ("%f*%d/1000 + %s + 0.01", f,d,s); JexlEngine jexlEngine = new JexlBuilder().create(); JexlExpression jexlExpression = jexlEngine.createExpression(expressionString); Double.valueOf(jexlExpression.evaluate(null).toString());Copy the code

perfect