You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by sebb <se...@gmail.com> on 2012/08/22 11:48:40 UTC

Re: [JEXL] unexpected expression evaluation

This list is shared by lots of components; please use the appropriate
prefix (added to this message) otherwise it may be overlooked by those
who can help.

On 22 August 2012 07:01, miten mehta <im...@yahoo.com> wrote:
> 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.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org