<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=giovanni.dalloglio" rel="nofollow">Giovanni Dall'Oglio Risso</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-8716?redirect=false" rel="nofollow">SPR-8716</a></strong> and commented</p>
<p dir="auto">The number comparison can be more accurate with BigDecimals (possibly also BigIntegers) and other number types:</p>
<p dir="auto">This test fail, becouse the BigDecimal is converted to Integer, truncating the value to zero:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="    @Test
    public void testGT() throws Exception
    {

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

        Assert.assertTrue(value);
    }"><pre class="notranslate"><code class="notranslate">    @Test
    public void testGT() throws Exception
    {

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

        Assert.assertTrue(value);
    }
</code></pre></div>
<p dir="auto">The responsible is the class org.springframework.expression.spel.ast.<strong>OpGT</strong> (but all the similar classes has the same imprinting)</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
     Object left = getLeftOperand().getValueInternal(state).getValue();
     Object right = getRightOperand().getValueInternal(state).getValue();
     if (left instanceof Number &amp;&amp; right instanceof Number) {
          Number leftNumber = (Number) left;
          Number rightNumber = (Number) right;
          if (leftNumber instanceof Double || rightNumber instanceof Double) {
               return BooleanTypedValue.forValue(leftNumber.doubleValue() &gt; rightNumber.doubleValue());
          } else if (leftNumber instanceof Long || rightNumber instanceof Long) {
               return BooleanTypedValue.forValue(leftNumber.longValue() &gt; rightNumber.longValue());
          } else {
               return BooleanTypedValue.forValue(leftNumber.intValue() &gt; rightNumber.intValue());
          }
     }
     return BooleanTypedValue.forValue(state.getTypeComparator().compare(left, right) &gt; 0);
}"><pre class="notranslate"><code class="notranslate">@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
     Object left = getLeftOperand().getValueInternal(state).getValue();
     Object right = getRightOperand().getValueInternal(state).getValue();
     if (left instanceof Number &amp;&amp; right instanceof Number) {
          Number leftNumber = (Number) left;
          Number rightNumber = (Number) right;
          if (leftNumber instanceof Double || rightNumber instanceof Double) {
               return BooleanTypedValue.forValue(leftNumber.doubleValue() &gt; rightNumber.doubleValue());
          } else if (leftNumber instanceof Long || rightNumber instanceof Long) {
               return BooleanTypedValue.forValue(leftNumber.longValue() &gt; rightNumber.longValue());
          } else {
               return BooleanTypedValue.forValue(leftNumber.intValue() &gt; rightNumber.intValue());
          }
     }
     return BooleanTypedValue.forValue(state.getTypeComparator().compare(left, right) &gt; 0);
}
</code></pre></div>
<p dir="auto">In order you:</p>
<ul dir="auto">
<li>check if is a Double</li>
<li>check if is a Long</li>
<li>treat it as an Integer</li>
</ul>
<p dir="auto">I attach my modest suggestion</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 3.0.5</p>
<p dir="auto"><strong>Attachments:</strong></p>
<ul dir="auto">
<li><a href="https://jira.spring.io/secure/attachment/18819/OpGT.java" rel="nofollow">OpGT.java</a> (<em>3.08 kB</em>)</li>
</ul>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398117559" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/13802" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/13802/hovercard" href="https://github.com/spring-projects/spring-framework/issues/13802">#13802</a> Support BigDecimals with SpEL (<em><strong>"duplicates"</strong></em>)</li>
</ul>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=obecker" rel="nofollow">Oliver Becker</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-9164?redirect=false" rel="nofollow">SPR-9164</a></strong> and commented</p>
<p dir="auto">When doing number arithmetic in SpEL the result type is apparently one of double, long or int.<br>
This has the unwanted effect that for example float or BigDecimal values will be changed to int.</p>
<p dir="auto">new java.math.BigDecimal("12.34")<br>
evaluates to 12.34</p>
<p dir="auto">-(new java.math.BigDecimal("12.34"))<br>
evaluates to -12</p>
<p dir="auto">see org.springframework.expression.spel.ast<br>
OpPlus, OpMinus, OpMultiply, OpDivide</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 3.1.1</p>
<p dir="auto"><strong>Reference URL:</strong> <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="4587405" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/80" data-hovercard-type="pull_request" data-hovercard-url="/spring-projects/spring-framework/pull/80/hovercard" href="https://github.com/spring-projects/spring-framework/pull/80">#80</a></p>
<p dir="auto"><strong>Attachments:</strong></p>
<ul dir="auto">
<li><a href="https://jira.spring.io/secure/attachment/19821/SpELFloatLiteralTest.java" rel="nofollow">SpELFloatLiteralTest.java</a> (<em>1.24 kB</em>)</li>
</ul>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398114688" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/13358" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/13358/hovercard" href="https://github.com/spring-projects/spring-framework/issues/13358">#13358</a> Expression language not compare BigDecimals with integers (<em><strong>"is duplicated by"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398117750" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/13832" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/13832/hovercard" href="https://github.com/spring-projects/spring-framework/issues/13832">#13832</a> SpEL: OpEQ should use equals()</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398164627" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/15943" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/15943/hovercard" href="https://github.com/spring-projects/spring-framework/issues/15943">#15943</a> Downgrade accidental use of Java 1.7 APIs</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398151045" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/14121" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/14121/hovercard" href="https://github.com/spring-projects/spring-framework/issues/14121">#14121</a> Add SpEL support for float literals</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398154260" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/14546" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/14546/hovercard" href="https://github.com/spring-projects/spring-framework/issues/14546">#14546</a> SpEL's arithmetic operations should explicitly detect BigInteger/Short/Byte and fall back to double handling for unknown Number subtypes</li>
</ul>
<p dir="auto">1 votes, 7 watchers</p>