简体   繁体   中英

Android rhino calculation strange behavior

I'm using this project ( https://github.com/APISENSE/rhino-android ) to make calculations in my Android app.

However, some numbers starting with 0 in the expression causes a strange behavior in the calculation. For example:

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

Other examples that looks ok:

  • eval(018+2) returns 20.
  • eval(019+5) returns 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.

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.

So the behavior here seems completely normal for JavaScript. So I need to treat my expression to avoid this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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