[
https://issues.apache.org/jira/browse/JEXL-420?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17815391#comment-17815391
]
Henri Biestro edited comment on JEXL-420 at 2/7/24 6:24 PM:
------------------------------------------------------------
It is always possible to use an arithmetic (ie a JexlArithmetic derived class)
of your choosing.
In your case, you might want to override the method:
{code:java}
public double toDouble(final Object val){code}
so it returns NaN when encountering a String.
Alternatively, you could override:
{code:java}
protected int compare(final Object left, final Object right, final JexlOperator
operator) {
try {
return super.compare(left, right, operator);
} catch(ArithmeticException xa) {
if (EQ.equals(operator)) {
return Integer.MAX_VALUE;
}
throw xa;
}
}{code}
to return a comparison value leading to false for the operator when catching an
ArithmeticException.
As for the general case, I'd rather not weaken the type system; the empty
string is already an (historic) convenient exception.
was (Author: henrib):
It is always possible to use an arithmetic (ie a JexlArithmetic derived class)
of your choosing.
In your case, you might want to override the method:
{code:java}
public double toDouble(final Object val){code}
so it returns NaN when encountering a String.
Alternatively, you could override:
{code:java}
protected int compare(final Object left, final Object right, final JexlOperator
operator){code}
to return a comparison value leading to false for the operator when catching an
ArithmeticException.
As for the general case, I'd rather not weaken the type system; the empty
string is already an (historic) convenient exception.
> Error while comparing float and string value
> --------------------------------------------
>
> Key: JEXL-420
> URL: https://issues.apache.org/jira/browse/JEXL-420
> Project: Commons JEXL
> Issue Type: Bug
> Reporter: Xu Pengcheng
> Priority: Major
>
> code:
> {code:java}
> 1.2 == ''{code}
> returns false
>
> code:
> {code:java}
> 1.2 == 'a'{code}
> causes exception
> {code:java}
> Caused by: java.lang.ArithmeticException: Double coercion: (a){code}
>
> Is it possible to check the right side is a number like string and return
> false if not?
--
This message was sent by Atlassian Jira
(v8.20.10#820010)