# Make a formula from a String into a number

**URL:** <https://discourse.processing.org/t/make-a-formula-from-a-string-into-a-number/32452>\
**Category:** Coding Questions\
**Created:** [September 27, 2021, 7:02pm UTC](https://discourse.processing.org/t/make-a-formula-from-a-string-into-a-number/32452 "2021-09-27T19:02:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)\
**Post date:** [September 27, 2021, 7:02pm UTC](https://discourse.processing.org/t/make-a-formula-from-a-string-into-a-number/32452/1 "2021-09-27T19:02:41Z")

</div>

I’m currently trying to make a problem to calculate a Expression in a String. Until now I used a code like this:

```auto
final ScriptEngine js=new ScriptEngineManager().getEngineByName("js");
String eq="5*(2+1)";
try{
	double eg=(((Double) js.eval(eq)).doubleValue());
	println(eg);
	}
	catch(ScriptException c){
	throw new RuntimeException(c);
	}

```

But is there another way to do this that doesn’t involve using a js interpreter?

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [September 27, 2021, 8:25pm UTC](https://discourse.processing.org/t/make-a-formula-from-a-string-into-a-number/32452/2 "2021-09-27T20:25:39Z")

</div>

as far as i know eval is evil. [this page](http://www.cs.man.ac.uk/~pjj/cs212/fix.html) will give you all the lingo you need to research how to evaluate expressions

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 28, 2021, 6:45am UTC](https://discourse.processing.org/t/make-a-formula-from-a-string-into-a-number/32452/3 "2021-09-28T06:45:12Z")

</div>

> [@NumericPrime](#):
>
> But is there another way to do this that doesn’t involve using a js interpreter?

> **[Quarks Place](http://lagers.org.uk/jasmine/)**
>
> Jasmine with Processing

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [September 28, 2021, 3:23pm UTC](https://discourse.processing.org/t/make-a-formula-from-a-string-into-a-number/32452/4 "2021-09-28T15:23:21Z")

</div>

If your use case is things like `"5*(2+1)"` then the Jasmine library by @quark – that @GoToLoop linked to above – is definitely the way to go. It is expressive and blazing-fast.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [September 28, 2021, 4:15pm UTC](https://discourse.processing.org/t/make-a-formula-from-a-string-into-a-number/32452/5 "2021-09-28T16:15:41Z")

</div>

This sketch shows how to use Jasmine to calculate `5*(2+1)` and also how you can evaluate the expression `a*(b+c)` for different values.

```auto
import org.quark.jasmine.*;

String strExpr = "5*(2+1)";
Expression expr = Compile.expression(strExpr, false);
int v = expr.eval().answer().toInteger();
println(v);

// Can also create a general solution to a * ( b + c)
strExpr ="a * (b + c)";
expr = Compile.expression(strExpr, false);
v = expr.eval(5, 2, 1).answer().toInteger();
println(v);
// Now solve 16 * (10 - 5)
v = expr.eval(16, 10, -5).answer().toInteger();
println(v);

```

should be enough to get you started.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [October 19, 2021, 10:48am UTC](https://discourse.processing.org/t/make-a-formula-from-a-string-into-a-number/32452/6 "2021-10-19T10:48:01Z")

</div>

I have no idea whether the OP made use of Jasmine but this discussion made me revisit the library and play with Java bytecode again.

So I have updated the library with new examples (only two before 😇) and added some new methods to those recognised by Jasmine.

Anyway you should be able to install Jasmine 1.1 through the Contributions Manager in a couple of days.

---

<div class="post-metadata">

**Author:** ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)\
**Post date:** [October 19, 2021, 4:43pm UTC](https://discourse.processing.org/t/make-a-formula-from-a-string-into-a-number/32452/7 "2021-10-19T16:43:44Z")

</div>

Yes in the end I made use of it! I’m exited to see the other features the library has to offer!
