commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sebb <seb...@gmail.com>
Subject Re: [JEXL] unexpected expression evaluation
Date Wed, 22 Aug 2012 09:48:40 GMT
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 <imiten@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


Mime
View raw message