Convert string to number

I have an Ionic Toggle and I want to convert its value to a 1/0 for true/false to use in a math expression

So I have

Ionic Toggle Organizer is checked:formatted-as 1/0

but that is a string and can’t take part in calculations (maybe it should be more clever there and allow it)

so I say let’s call :extract on this, but that one only has E-mail option, it can’t extract Numbers out of strings

Is there any way I can do this? (get a 1/0 number from the checked state of an ionic toggle). One though is to use some hidden element with a conditional to sync to the toggle element, but not sure which element can give me a number (maybe get the index of the selection in a dropdown synced to the toggle or something?)

I could talk to my DB (the Ionic Toggle sets or deletes an object from a field) and use “is not empty” to check the same thing, but then I have the same problem, how to convert a Boolean expression like is not empty to a number (I’d use formatted-as and extract, but then extract doesn’t support extracting numbers from strings - or is there some convert that can give me a 0/1 or at least a 0/-1 number from a Boolean?)

I want the top star rating to display (it is readonly - don’t like its disabled hover cursor, but will put a transparent group or button over it to stop it showing the not-allowed cursor) the average of the other star ratings (which calculate their values from some 5-points-ratings from the database [clicking on them takes one to star rating pages per role of a team member, have a transparent button over them]).

So I now put a conditional in each of those 5 star ratings to set them to value 5 when the toggle switch is unchecked (in that case they also disappear by turning the button over them to not-clickable and non-transparent based on the respective toggle switch). That way I hope the top rating that does the average of those ratings will ignore the unselected ones (since they’ll have value 5 [max])

what I’m not sure is the order in which elements get updated in Bubble when I have a rule in the “initial value”, that is will the other ratings first get calculated and then the one whose formula depends on their values? Is Bubble clever enough to see that the formula creates a dependency? If not, in which order are they updated? Do I need to set their names to something? Or is it related to the order I had added them to the designer?

the workaround I devised works nicely as show below (the hidden star ratings for which the toggle switches are off get a 5 via a conditional that sets their initial value - this seems to trigger an update to the top star rating that shows the average for the student by summing all those star rating values [5 roles] and dividing by 5)

the initial questions/suggestion I have at the top (on how to make a boolean or a string into a number) still stands though

1 Like

unfortunately I didn’t think the math there correctly, I can’t add 5 stars for non-selected roles and then divide by 5 roles, since when one has two roles with both having 0 score they end up getting an average that is not 0, but 0+0+5+5+5 / 5 which gives 3 stars instead of 0 stars

so since I can’t convert a string to a number, my next idea is to concatenate some chars that are produced using :formatted-as (say * for true and nothing for false) and then to get the length (count the chars) of that string in order to count the selected roles so that I can divide by their count the score sum of all roles (the non-selected ones would then have 0 stars, not 5 as I had hacked above)

however, I’m wondering how easy it will be to make such an expression (not sure if :formatted-as applies to the whole expression chain or just to the last member of it if I try to concatenate multiple strings produced by IsChecked expressions

could alter my data type there (RatingRoles) and add a RolesCount field. Then at each toggle on increase it and at each toggle off event decrease it. I just have to fix the value for existing objects somehow by hand or toggle off all their roles (that would lose their scores though and one would have to re-enter them) and toggle them on again to get counted

thinking of that create-a-string-with-length-N using formatted-as for each toggle to add a char if turned on and not emit when not turned on, then get its length could work. I can make that string in a step using multiple dynamic expressions placed in a row and then at next action/step count the previous step’s result’s string length and keep it in a database field. Could call that in a CountRoles custom event that I call at each toggle on and each toggle off to update the count and/or that I call before calculating the student score (which I now do using a timer [which isn’t the best way but couldn’t find other])

It seems one can indeed convert a Boolean to a string with

someBooleanExpression:formatted-as */ :number-of-characters

they get 1 for true and 0 for false

if they have multiple Booleans and just want to count how many are true (not which) they can make a string with * for each true one (place multiple dynamic expressions one after the other) and put it in the database, then read it at other step and count the number of characters (to store at yet another db field for example and use later on)

took me some time to figure out though

1 Like