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

计算公式中,一个是数据,一个是 字符串,计算会抛异常 #45

Closed
viplq opened this issue Feb 8, 2018 · 6 comments
Closed

Comments

@viplq
Copy link

viplq commented Feb 8, 2018

"8161 == PCV3-8" ,运行时,发现8161是数据,会走数字类型,但是比较值是字符串时报错。

问题点:
if (otherValue instanceof Number) {
return this.innerCompare(AviatorNumber.valueOf(otherValue));
} else {
throw new ExpressionRuntimeException("Could not compare " + this + " with " + other);
}

@killme2008
Copy link
Owner

你好,能否将问题具体一些?或者给一段可以重现的代码。

@viplq
Copy link
Author

viplq commented Feb 9, 2018

Expression compiledExp = AviatorEvaluator.compile("standardValue == compareValue");
Map<String, Object> params = new HashMap<String, Object>();
params.put("standardValue", 8161);
params.put("compareValue", "PCV3-8");

    System.out.println(compiledExp.execute(params));

@viplq
Copy link
Author

viplq commented Feb 9, 2018

我修改源码 AviatorNumber:
希望能给个正确修改的版本。
if (otherValue instanceof Number) {
return this.innerCompare(AviatorNumber.valueOf(otherValue));
} else if (otherValue instanceof String) {
return new AviatorString(String.valueOf(number)).compare(other, env);
} else {
throw new ExpressionRuntimeException("Could not compare " + this + " with " + other);
}

@killme2008
Copy link
Owner

这个错误是符合预期的,你传入的是数字和字符串,两者比较就是要报错的。

具体比较规则参见文档

https://github.com/killme2008/aviator/wiki#%E5%85%B3%E7%B3%BB%E8%BF%90%E7%AE%97%E7%AC%A6

@killme2008
Copy link
Owner

针对这个问题,你有几个解决办法:

  1. 保证传入的类型是相同的,也就是传入之前对 standardValue 做 toString 处理。
  2. 可以用 str 函数将某个值强制转成字符串来比较 str(standardValue) == compareValue

@viplq
Copy link
Author

viplq commented Feb 14, 2018

谢谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants