You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Даниил <da...@gmail.com> on 2012/04/09 09:53:15 UTC
[jexl]
I'm trying to evaluate the expression 2.3% of value and I got invalid
result:
My code is follow:
BigDecimal bd = new BigDecimal("325");
JexlEngine jexl = new JexlEngine();
String expr = "2.3*SO/100";
JexlContext jc = new MapContext();
jc.set("SO", bd);
Object evaluate = jexl.createExpression(expr).evaluate(jc);
System.out.println(evaluate);
produce the result: 7.474999999999999 which is incorrect.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: [jexl]
Posted by henrib <he...@apache.org>.
Howdy;
I suppose you are expecting the result to be a BigDecimal ?
Something like:
public void test131() throws Exception {
BigDecimal sevendot475 = new BigDecimal("7.475");
BigDecimal SO = new BigDecimal("325");
JexlContext jc = new MapContext();
jc.set("SO", SO);
JexlEngine jexl = new Engine();
String expr = "2.3*SO/100";
Object evaluate = jexl.createExpression(expr).evaluate(jc);
assertEquals(sevendot475, (BigDecimal) evaluate);
}
If so, the JexlArithmetic can be specialized/derived (or patched) to get
this result by overriding (changing) the code in
JexlArithmetic.{add,subtract,multiply,divide,mod} to check first for
BigDecimal rather than floating point numbers as in:
public Object add(Object left, Object right) {
if (left == null && right == null) {
return controlNullNullOperands();
}
try {
// if either are bigdecimal use that type
if (left instanceof BigDecimal || right instanceof BigDecimal) {
BigDecimal l = toBigDecimal(left);
BigDecimal r = toBigDecimal(right);
BigDecimal result = l.add(r, getMathContext());
return narrowBigDecimal(left, right, result);
}
// if either are floating point (double or float) use double
if (isFloatingPointNumber(left) || isFloatingPointNumber(right))
{
double l = toDouble(left);
double r = toDouble(right);
return new Double(l + r);
}
// otherwise treat as integers
BigInteger l = toBigInteger(left);
BigInteger r = toBigInteger(right);
BigInteger result = l.add(r);
return narrowBigInteger(left, right, result);
} catch (java.lang.NumberFormatException nfe) {
// Well, use strings!
if (left == null || right == null) {
controlNullOperand();
}
return toString(left).concat(toString(right));
}
}
Hope this helps,
Cheers
Henrib
--
View this message in context: http://apache-commons.680414.n4.nabble.com/jexl-tp4542792p4544885.html
Sent from the Commons - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
RE: [jexl]
Posted by Jason Dorsey <Ja...@netiq.com>.
From what I am seeing below this come out to :
"2.3 multiplied by 325 divided by 100" Or "2.3 * 3.25"
And that does equal your result.
I think you should be multiplying by 0.023 instead of 2.3.
Jason Dorsey | Engineering Intern
1233 West Loop South
Houston, TX 77027 USA
Jason.Dorsey@netiq.com
www.attachmate.com | www.netiq.com
-----Original Message-----
From: Даниил [mailto:danilkus@gmail.com]
Sent: Monday, April 09, 2012 2:53 AM
To: user@commons.apache.org
Subject: [jexl]
I'm trying to evaluate the expression 2.3% of value and I got invalid
result:
My code is follow:
BigDecimal bd = new BigDecimal("325");
JexlEngine jexl = new JexlEngine();
String expr = "2.3*SO/100";
JexlContext jc = new MapContext();
jc.set("SO", bd);
Object evaluate = jexl.createExpression(expr).evaluate(jc);
System.out.println(evaluate);
produce the result: 7.474999999999999 which is incorrect.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org