Simple math Expression for bubble

I am setting up a thing where people can select an option from a drop down and when they select it that optin has a preset value in it (amount 1A) and their is a quantity button that allows them to select how many per that drop down their are. Their is 6 different drop downs I am trying to make it so when they put an option for all 6 drop downs it will do simple math and do say (amount 1a * quantity) + (Amount 1b * quantity) etc. = total amount

but the amounts while change according to what drop down you select so it would need to be a dynamic value. How would I go about doing simple math with values like this

1 Like

There are a couple easy ways to solve this:

  • You can directly reference the fields within Bubble and do basic math on them. Note - Bubble evaluates from left to right (i.e., it’d ignore the parenthesis in your math equation so the order of operations would be different and the end result would be different). To get around this, you can set a custom state to be equal to, say, Amount 1B * quantity (and the same for other sub calculations). Then, where you’re doing the math at the end you could add up the results of those custom-states. That’s probably the easiest way.
  • As an alternative, I believe it’s the CSS Tools plugin that provides a math expression option that’ll give you a bit more power. So, that’s another option. Also quite easy to implement.

One last thought - even though it’s not what you asked, I think this will be helpful to know:

  • If you want to include drop downs that don’t just include numbers, but perhaps also include some text so that they’re more user friendly (e.g., rather than showing 3, 4, or 5 you could show 3 people, 4 people, or 5 people). If you add text, you’ll need to translate these values into numbers. The easy way is to run a workflow when the input’s value is changed and use an action to set a custom-state that’s a number based on the input the use selected (e.g., if input’s value is 3 people, set custom-state-A = 3).
3 Likes

OR, make a new datatype that has everything you need in one object: the dropdown label, any value(s) represented by that dropdown, etc.

Most new users don’t realize this. Bubble’s dropdowns are DESIGNED to be used with custom data types, NOT just primitive built-in types.

In native HTML, a dropdown has a LABEL and a corresponding VALUE. At first, it can seem like Bubble’s dropdowns are “broken” in comparison. But this is not correct. What you’re supposed to do is create a thing that has a label and corresponding value and use THAT as the dynamic source for the dropdown.

(For general info on this concept search anything I’ve written where I talk about “system constants” or “system variables”.)

1 Like

So this is what I have and the words on the bottom is the highlighted part and im not sure what you guys are referring to. and im not sure what to do as far as its a dynamic expression

@sridharan.s @keith based off what I put above any ideas?

SO I Posted what is going on above any way that I can get some help

Perhaps try the Toolbox plugin, and add an expressions element to the page. That should be a really easy way to do what you’re looking to do.

1 Like

Gonna try it out and Ill get back to you thanks for the support for the time being

charge%201 Outcome

this is what I have but it is still not giving me an outcome and if I go into preview and do inspect it is saying the list item expression is = to 0

poor bubble forum dwellers, I have been posting how badly bubble needs work in the area of math. But people have been patient with my bitching but here is what I have learned:

  1. use math.js or toolbox expressions to do math, because they (poorly) let you use parentheses)

  2. when using those, bubble math (the blue stuff) gets evaluated before non-bubble math (the white stuff)

  3. without math.js, bubble evaluates left to right NOT in order of priority like normal math ( for example 5+5*2 = 20, it does not equal 15)

  4. Using parentheses is a total PITA. The key is to type the parens first “()” then edit in and around them afterwards.

I think this is the best I can offer

1 Like

I do not get the plugins unfortunately is the expression from the toolbox where I put in the values from the database? or in the listitemexpression it doesn’t make sense

@jwharrod2000 Can you explain the definition of “Charges 1 - arrest’s value’s Fine Total”?

Assuming you want to continue with six preset rows and preset dropdowns …

It looks like you aren’t using any lists, so List Item Expression isn’t going to be useful.

Try these steps:

  • Expression element with

Charges1qty * Charges1amt + Charges2qty * Charges2amt + Charges3qty * Charges3amt + Charges4qty * Charges4amt + Charges5qty * Charges5amt + Charges6qty * Charges6amt

Then replace each ChargesNqty with a dynamic expression that selects the dropdown’s value, replace each ChargesNamt with a dynamic expression that selects the appropriate amount.

Alternatively replace qty * amt with a single dynamic expression multiplying them with Bubble’s operator.

  • Text element to show Expression’s value.

For more reading on Toolbox: Toolbox plugin - collection of utility elements
Examples in: https://bubble.io/page?id=toolbox-example

So in my database I have Penal code and the values under penal code are the penal code it self the jail time and the fine amount, where you enter that penal code in the form that I have posted previously on the pink background is called “charges#” obviously # is for 1-6 so basically the dynamic data refers back to the charge which links to the penal code in the database and whatever code it refers to will have a specific jail time and fine time that links to the code @mishav

1 Like

Now that I have the math correct and it does it is there a way to set a default 0 value for the search bars because sometimes out of the 6 options not all of them are filled in and according to the math expressions unless their is a value it will not output the total and I do not want to have my team have to input a 0 value every time I want to just default it to one but when I go into the search bar and try to set it to default it will only let me set elements as defaults. So I figured maybe if I set a QTY drop down to 0 and had it default to that I could have the search bar default off that but it is incorrect

The Conditional tab is good for setting up a different default if some other element’s value is empty or not.

So I would in expression a under conditional do when " Charges 1(PENAL CODE) isn’t valid" expression charges 1’s value is = 0?

Is there a way to default the value to a database value because this seems like alot of going around @mishav

Perhaps you can explain what is going around … do you have a circular buffer?

Hint - keep us guessing and we’ll keep guessing.

So when someone types into the “penal code” section it pops up with a selection and basically what ever you typed in it will give you the closest result like say I have a code in the data bade called “Charge test” and I type in “char” it will automatically come up with “Charge test” and so I can click on that and it will have the values for “charge test put into the fine amount and jail amount boxes”

So the expression when I do

Charges1qty * Charges1amt + Charges2qty * Charges2amt + Charges3qty * Charges3amt

= totalamt

if Charges1amt is not selected then it is not a number and the math plugin will not do the math

So what I want to do is set the penal code section to default to a database value that will fill in the Charges1amt to 0 so that the math plugin can do it

1 Like

Figured it out, set the default value to “Do a search for” And link the database