Search code examples
javajexl

JEXL evaluate returns int


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?


Solution

  • 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