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

This topic explains how to create a formula to convert a numeric value or a range of numbers into text. For more information on ways to format numbers through formulas, see https://msdn.microsoft.com/library/dwhawy9k(v=vs.110).aspx.

Consider the following form (see figure below), where the value entered in the Required Time field generates a corresponding text in the Complexity Level field.

 

 

In the example, the Required Time field is a Number attribute that accepts values from 0 to 100. The Complexity Level field is a Formula attribute with Text output, which is 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 = ((@required_time >= 70) ? 'High' :

           (@required_time >= 30) ? 'Medium' :

           (@required_time >= 0)  ? 'Low':

                                    'Not Informed');  

#result

 

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

 

In this case, if a value equal to or greater than 70 is entered for the Required Time attribute, the formula in the Complexity Level field will result in the "High" text. Likewise, a value equal to or greater than 30 (and less than 70) will result in "Medium", and a value less than 30 will result in "Low".

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 a condition is true or all the conditions are evaluated. If no conditions are met, the last remaining value will be used (in the example above, "Not Informed").