commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From miten mehta <imi...@yahoo.com>
Subject unexpected expression evaluation
Date Wed, 22 Aug 2012 06:01:03 GMT
Hi,

I am using commons-jexl 2.1.1 and noticed that expression evaluation for below to be different.

//returns 55.87999 incorrectly
formula = "#{(9/5 * celcius )+32}";
formula = formula.substring(2, formula.length()-1);//strip prefix #{ and suffix }
map.put("celcius", 23.88);

//returns 75 correctly
formula = "#{(celcius * 9/5)+32}";
formula = formula.substring(2, formula.length()-1);//strip prefix #{ and suffix }
map.put("celcius", 23.88);

Here is method used to evaluate:
public static Double evaluate(String formula, Map<String,Double> ctxParam) {
        JexlEngine jexl = new JexlEngine();
        org.apache.commons.jexl2.Expression e = jexl.createExpression(formula);
        JexlContext ctx = new MapContext();
        for(Map.Entry<String, Double> me : ctxParam.entrySet()) {
            ctx.set(me.getKey(), me.getValue().toString());
        }
        Object result = e.evaluate(ctx);
        return Double.parseDouble(result.toString());
    }

is there some rules for how we form the expression ?

Regards,

Miten.

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message