Loading repository data…
Loading repository data…
Keelar / repository
A simple mathematical expression evaluator for Kotlin and Java, written in Kotlin.
A simple mathematical expression evaluator for Kotlin and Java, written in Kotlin.
val result = Expressions()
.eval("(5+5)*10") // returns 100
You can define variables with the define method.
val result = Expressions()
.define("x", 5)
.eval("x*10") // returns 50
The define method returns the expression instance to allow chaining definition method calls together.
val result = Expressions()
.define("x", 5)
.define("y", "5*2")
.eval("x*y") // returns 50
Variable definition expressions can reference previously defined variables.
val result = Expressions()
.define("x", 5)
.define("y", "x^2")
.eval("y*x") // returns 125
You can add new functions with the addFunction method.
val result = Expressions()
.addFunction("min") { arguments ->
if (arguments.isEmpty()) throw ExpressionException(
"min requires at least one argument")
arguments.min()!!
}
.eval("min(4, 8, 16)") // returns 4
You can set the precision and rounding mode with setPrecision and setRoundingMode.
val result = Expressions()
.setPrecision(128)
.setRoundingMode(RoundingMode.UP)
.eval("222^3/5.5")