How to Create a Formula to Convert a Text into a Number

This topic explains how to create a formula to transform a text option from a List of Options attribute into a numeric value. For example, in the form below, the option selected in the Satisfaction Level field generates a corresponding value in the Approval Rate field.

 

  

 

In the example, the Satisfaction Level field is a single-selection List of Options attribute, with the "Low", "Medium", and "High" options. The Approval Rate field is a Formula attribute with a Number output, which is calculated based on the option selected in the previous field. The formula in this attribute follows the structure below. Note that this structure is similar to the traditional "if-then-else", but the "IF" function is substituted by the ternary operator "?".

#result = (@satisfaction_level=='Low' ? 1 :

              @satisfaction_level =='Medium' ? 2 :

              @satisfaction_level =='High'  ? 3 :

                                          -1 );

#result

 

Note: The "?"  ternary operator, even when used with other operators, should always begin and end with parentheses.

 

In this case, if the "Low" option is selected in the Satisfaction Level field, the formula of the Approval Rate field will generate the value 1.00. Likewise, the "Medium" option will result in the value 2.00 and "High" in 3.00.

Each line in the formula is only evaluated if the previous conditions are false. If the first condition is true, the first value is returned. Otherwise, the following ternary operator is evaluated and the process repeats itself until one is true or all the conditions are evaluated. If no condition is met, the last remaining value will be used (in the example above, "-1").