java 數據表達式解析工具MathParser

背景

工做遇到業務計算,規則常常變更,考慮配置數學表達式java

依賴

<dependency>
            <groupId>org.mariuszgromada.math</groupId>
            <artifactId>MathParser.org-mXparser</artifactId>
            <version>4.2.0</version>
        </dependency>

一、實例解析Math.min   解析min(days/10,1)。支持java Math中的計算方式

public static void main(String[] args) throws InterruptedException {
        Argument days = new Argument("days = 30");
        Expression min = new Expression("min(days/10,1)", days);
        mXparser.consolePrint(min.getExpressionString() + "----" + min.calculate());
        Expression e = new Expression("2-(32-4)/(23+4/5)-(2-4)*(4+6-98.2)+4");
        mXparser.consolePrintln("Res: " + e.getExpressionString() + " = " + e.calculate());
        Expression abs = new Expression("abs(-1)");
        mXparser.consolePrintln("Res: " + abs.getExpressionString() + " = " + abs.calculate());
        Expression sqrt = new Expression("sqrt(2)");
        mXparser.consolePrintln("Res: " + sqrt.getExpressionString() + " = " + sqrt.calculate());
     
    }

結果:[mXparser-v.4.2.0] min(days/10,1)----1.0 express

 

 

更多的實例

http://mathparser.org/mxparser-tutorial/simple-expressions/spa

相關文章
相關標籤/搜索