Convert Text to Number to use in formulas

Is it possible to convert Text option from a drop down into a number to use in a formula?

For eg. User selects 'Weekly, 'Monthly' or 'Yearly' from dropdown. I would then like to convert 'Weekly' into 1, 'Monthly' into 4 and Yearly into 52.18 to use in a formula.

Hi @latetotheparty
Try this:

{formmenu: name=window; Weekly; Monthly; Yearly}
{weeks= 1 if window=="Weekly" else (4 if window=="Monthly" else 52.18)}
total cost is {=weeks*20}

3 Likes

Dan beat me to it by a minute :slight_smile: my example is formatted slightly differently but accomplishes the exact same thing:

{formmenu: default=Weekly; Monthly; Yearly; name=frequency}
{if: frequency=="Weekly"; trim=yes}{frequencynumber=1}{elseif: frequency=="Monthly"}{frequencynumber=4}{elseif: frequency=="Yearly"}{frequencynumber=52.18}{endif: trim=left}

Menu into a value: {=frequencynumber}
Example of the value used in a formula: {=frequencynumber*5}

3 Likes

Thank you so much for your solutions! :smiling_face_with_three_hearts:
I was determined to solve it and came up with a different solution. Please correct me if I'm wrong.

Also, I had no idea I could create conditional variables. Thank you for that!

{formmenu: default=weekly; monthly; yearly; name=frequency; multiple=no}
Weekly cost:{=5*replace(replace(replace(frequency,"yearly",52.18),"monthly",4),"weekly",1)}

3 Likes

Wow, so many cool solutions. I like to drop my own version of the solution here.

{data=[
["name": "Weekly", "value": 1],
["name": "Monthly", "value": 4],
["name": "Yearly", "value": 52.18]
]}{formmenu: values={=data}; name=frequency; itemformatter=v->v["name"]; multiple=no}
Weekly cost: {=5*frequency["value"]}

4 Likes

Should I say I'm late to the party :wink: Either way, here's Vinod's solution, in a slightly concise version:

{costs=[
"Weekly":1,
"Monthly": 4,
"Yearly": 52.18
]}
{formmenu: values={=keys(costs)}; name=frequency; multiple=no}
Weekly cost: {=5*costs[frequency]}

Using keyed lists with the keys function.

4 Likes