0

I would like to achieve the following Code example so I can store the formula into database and reuse it:

Function fun = new Function("(${x}+${y})/${z}");            
Argument x = new Argument("x", BigDecimal.ValueOf("1.1"));
Argument y = new Argument("y", BigDecimal.ValueOf("1.1"));
Argument z = new Argument("y", BigDecimal.ValueOf("1.1"));
BigDecimal result = fun.calculate(x,y,z);

I have already tried other frameworks like commons-jexl and mathparser, but none of them support BigDecimal.

localhost-er
  • 113
  • 3
  • 10

2 Answers2

1

This Maven dependency:

        <dependency>
            <groupId>com.udojava</groupId>
            <artifactId>EvalEx</artifactId>
            <version>2.5</version>
        </dependency>

with this snippet:

    System.out.println(
        new Expression( "(x + y) / z" )
                .with( "x", BigDecimal.valueOf( 1.1 ) )
                .and( "y", BigDecimal.valueOf( 1.1 ) )
                .and( "z", BigDecimal.valueOf( 1.1 ) )
                .eval()
    );

prints:

2
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
0

Try this

<dependency>
    <groupId>com.jtransc</groupId>
    <artifactId>jtransc-rt</artifactId>
    <version>0.6.1</version>
</dependency>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Jake
  • 1
  • 1
  • 1
    Please add more information to the answer. – J Fabian Meier Dec 28 '20 at 13:36
  • 2
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Dharman Dec 28 '20 at 14:03