JBEE - Java Basic Expression Evaluator (library)
JBEE is yet another parser and evaluator for arithmetic expressions. It started out as a calculator component for TradeTrax, but quickly matured into a standalone project.
Why another EE library? Because writing one from scratch was faster/easier than dealing with the bugs/shortcomings of existing libraries:
- Open Source License
- JBEE is licensed under the terms of the APL 2.
- Self contained
- An expression parser already is a dependency. It should not add dependencies of it's own to any project.
- Arbitrary precision calculation
- An EE library must be suitable for doing financial calculations without precession loss. That is, use java.math.BigDecimal exclusively (surprisingly many EE libs get this wrong and use Java primitive types instead).
- Honor the user's Locale settings
- Depending on the locale, the result of 1/4 is either written as 0.25 or 0,25 (likewise, 10^3 may be written as 1,000 or 1.000) depending on the Locale. No other EE lib supports this.
- Getting precedence right with infix notation is notoriously difficult. Any parser that's not based on a formal (YACC) grammar is potentially doing it wrong (unit tests do not substitute for proving correctness)!
Using JBEE is as simple as it gets:
// Use default locale and MathLib Evaluator e = new Evaluator(); BigDecimal result = e.evaluate("3,000 * 4.0 + 0xFF"); System.out.println(result);