Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expression language not compare BigDecimals with integers [SPR-8716] #13358

Closed
spring-issuemaster opened this issue Sep 22, 2011 · 3 comments

Comments

@spring-issuemaster
Copy link
Collaborator

commented Sep 22, 2011

Giovanni Dall'Oglio Risso opened SPR-8716 and commented

The number comparison can be more accurate with BigDecimals (possibly also BigIntegers) and other number types:

This test fail, becouse the BigDecimal is converted to Integer, truncating the value to zero:

    @Test
    public void testGT() throws Exception
    {

        ExpressionParser ep = new SpelExpressionParser();
        Expression expression = ep.parseExpression("new java.math.BigDecimal('0.1') > 0");
        Boolean value = expression.getValue(Boolean.class);

        Assert.assertTrue(value);
    }

The responsible is the class org.springframework.expression.spel.ast.OpGT (but all the similar classes has the same imprinting)

@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
     Object left = getLeftOperand().getValueInternal(state).getValue();
     Object right = getRightOperand().getValueInternal(state).getValue();
     if (left instanceof Number && right instanceof Number) {
          Number leftNumber = (Number) left;
          Number rightNumber = (Number) right;
          if (leftNumber instanceof Double || rightNumber instanceof Double) {
               return BooleanTypedValue.forValue(leftNumber.doubleValue() > rightNumber.doubleValue());
          } else if (leftNumber instanceof Long || rightNumber instanceof Long) {
               return BooleanTypedValue.forValue(leftNumber.longValue() > rightNumber.longValue());
          } else {
               return BooleanTypedValue.forValue(leftNumber.intValue() > rightNumber.intValue());
          }
     }
     return BooleanTypedValue.forValue(state.getTypeComparator().compare(left, right) > 0);
}

In order you:

  • check if is a Double
  • check if is a Long
  • treat it as an Integer

I attach my modest suggestion


Affects: 3.0.5

Attachments:

Issue Links:

  • #13802 Support BigDecimals with SpEL ("duplicates")
@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Sep 22, 2011

Giovanni Dall'Oglio Risso commented

Sorry, in the attachment i found an error on
if ( leftNumber instanceof BigDecimal || -leftNumber- rightNumber instanceof BigDecimal )

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Apr 16, 2012

Giovanni Dall'Oglio Risso commented

Hi...

I'm the reporter for this issue.

We managed to solve this problem, adding BigDecimals; we also made a pull request: #54

You can take that pull request, or start by that code (we added quite test cases).

PS: maybe you can consider to add also the BigIntegers support...

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented May 14, 2012

Chris Beams commented

Resolving as a duplicate of #13802.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.