# Official way of doing computations in Bubble

Hi,

What is the official way of computing in Bubble?

I want to compute pricing of an item which depends on certain factors.

I have tried following so far and have been facing issues in all of those:

1. Assigning value to a field of a DB thing in workflow: Bubble doesn’t allow any computation there
2. Run Javascript of Toolbox plugin: Facing an issue with the JS to Bubble element there
3. Use “Expressions” from Toolbox plugin: Facing some issues there too with fields being empty and me not being able to put conditions there effectively. Also, it was the ugliest way as I have to create many expressions and basically code full pricing logic using workflow boxes while actual JS code was just about 10 lines.
4. Use “Server Script” of Toolbox plugin: Some issue here too. Getting error of “properties.thing1.get not a function”

While it would be great if someone could guide me in fixing any of these issues so that I get unblocked, but I am pretty sure there is some official way that Bubble supports for doing such computations? It can’t be dependent on plugin created by someone?

Thanks,
Mukesh

Bubble does allow (1) - it would cover most (simple-ish) use cases.

What are you trying to calculate?

Somehow I didn’t see the options of "+ - * " etc. that come in the dropdown.

What I was trying was to enter * itself and then it won’t move forward. That made me feel that * is not possible.

This one is also of course very limited, as looks like parenthesis are not allowed. But I guess I should be able to manage without that with some ugly work.

Thanks again.

1 Like

For parenthesis, just remember that the calculation is done left to right, so the next statement always evaluates the outputs of the previous statements. Keeping this in mind you can actually build long, complex calculations.

So parenthesis are allowed? Or you are saying that parenthesis effect can somewhat be achieved utilising the way Bubble computes?

Could you give an example?

What I am trying to do is something like this:

t = ((a * b * c) * (1-d) / 100) * (1+e) / 100

No. I think what @exception-rambler is saying is that there is no “conventional” operator precedence; nor can parentheses be used to enforce precedence. Instead, the expression is simply evaluated from left to right. So, for instance…

`7 + 3 * 2`

…would evaluate to 20 and not 13.

EDIT: Or are parentheses allowed? Hmm, that’s actually something I need to test. I might be wrong here.

1 Like

Yeh it’s not possible unfortunately - it’s an ongoing ask on this thread: Order of operations

If I was doing your calculation @mghatiya I would personally follow the approach that I recommended here, because with a few lines of JS you can clear up some the edge cases - but it’s definitely not ideal that you can’t handle this more straightforwardly.

1 Like

Thanks Edward. As mentioned in my original post, I did try approach of using “Run Javascript” along with “JS to Bubble”.

But I had run into issues and found no solution. More details of that are in this thread.

I have just commented on that thread what the issue is - you’re using quotation marks around your variable

This raises an interesting question about what a “better” approach to mathematical expressions might look like in Bubble. IOW, what might be a more friendly UI for creating simple math expressions?

It seems, ideally, one would be able to simply type an expression using parentheses but somehow reference “variables” representing the inputs. Given Bubble’s focus on workflows and app logic, mathematical expressions are one place where Bubble’s UI “gets in the way”, and resorting to JS is far from ideal.

Has anyone given this any thought?

It would be cool to click a button “Calculate expression” and then have like a dial-pad/calculator widget thing popup. Then from there you can use it as a regular calculator, but also reference input values and custom states.

Maybe the current set up can’t handle parentheses because it could too easily be considered part of a string

1 Like