简体   繁体   English

Android rhino计算奇怪的行为

[英]Android rhino calculation strange behavior

I'm using this project ( https://github.com/APISENSE/rhino-android ) to make calculations in my Android app. 我正在使用这个项目( https://github.com/APISENSE/rhino-android )在我的Android应用程序中进行计算。

However, some numbers starting with 0 in the expression causes a strange behavior in the calculation. 但是,表达式中以0开头的某些数字会导致计算中出现奇怪的行为。 For example: 例如:

  • eval(016+2) returns 16. eval(016 + 2)返回16。
  • eval(016) returns 14. eval(016)返回14。
  • eval(031) returns 25. eval(031)返回25。
  • eval(031x2) returns 50. eval(031x2)返回50。

Other examples that looks ok: 其他看起来不错的例子:

  • eval(018+2) returns 20. eval(018 + 2)返回20。
  • eval(019+5) returns 24. eval(019 + 5)返回24。

Code sample: 代码示例:

String expression = "016+2";
ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino");
double result = (double) engine.eval(expression);

Any ideas? 有任何想法吗?

In JavaScript, putting a 0 in front of an integer will cast it to octal, as well as 0x will cast it as hexadecimal and 0b as binary. 在JavaScript中,将0放在整数前面会将其转换为八进制,以及0x将其转换为十六进制,0b转换为二进制。

This behavior does not take place if there is an 8 or a 9 in your integer since the cast to octal is impossible in this case. 如果整数中有8或9,则不会发生此行为,因为在这种情况下,转换为八进制是不可能的。

So the behavior here seems completely normal for JavaScript. 所以这里的行为似乎完全正常的JavaScript。 So I need to treat my expression to avoid this. 所以我需要对待我的表达来避免这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM