Library written in Kotlin to help evaluate mathematical expressions that can be extended by custom operators and functions.
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.cregus:expression-evaluator:1.0.2'
}
println(evaluate("2+2")) // Prints 4
println(evaluate("round(10/3,2)")) // Prints 3.33
println(evaluate("x*y", data = mapOf("x" to 2, "y" to 3))) // Prints 6
Tokenizer.registerFunction("random") { a, b -> Random.nextInt(a.toInt(), b.toInt()).toBigDecimal() }
println(evaluate("random(1,6)")) // Prints random number between 1 (inclusive) and 6 (exclusive)