I hava use jeval to get result from an expression string,but I find that sometimes I get the wrong result.So I try to use the BigDecimal,it is Ok,but if my express is more complex like (0.1+0.2)*10+15/3
How can I encapsulated into a function,do any jar or lib that have this function?
The code is here:
Evaluator eval = new Evaluator();
String expression="(0.1+0.2)";
String rest = eval.evaluate(expression);
System.out.println(new BigDecimal(rest)); //0.30000000000000004
Here is my own function:
private String getResult(String expression){
java.math.BigDecimal b = new java.math.BigDecimal("0.1");
java.math.BigDecimal c = new java.math.BigDecimal("0.2");
return b.add(c);
}