JEXL evaluate returns int instead of float:
JexlEngine jexl = new JexlEngine();
Expression e = jexl.createExpression("7/2");
Float result = (Float)e.evaluate(null);
I receive this error:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Float
Can I change a setting to return a float?
Answer 1
7/2
expression will evaluate to int result and so it is failing to cast Integer to Float, if you want it to be resulting in float you need to change expression to 7 / 2.0F
Answer 2
To be accurate you must convert to Float any of your parameters, so use any of these (7 / 2F)
or (7F / 2)
.
However, due to Java's auto-unboxing, your could avoid Exception in your initial code, but unfortunately lose in precision, if you use
Float result = (float)e.evaluate(null);
Another method that will work is casting to Double, so (7 / 2D)
or (7D / 2)
and then use
Float result = e.evaluate(null).floatValue();