How to Create a Formula to Format a Number as Currency

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

For example, consider the following form (see figure below), where the value entered in the Numeric Value field generates a corresponding text in the Monetary Value field, according to the option selected in the Currency field.

 

 

In this case, the Currency field is a single-selection List of Options attribute with the following options: British pound, Brazilian real, and US dollar. The Numeric Value field is a Number attribute, and the Monetary Value field is a Formula attribute with Text output, which is based on the values provided for the two previous fields, using the structure below. Keep in mind that, although the monetary value is expressed numerically, the formula treats this value as a text and, therefore, it is important for the Text output to be selected so the expression can be validated correctly. Note that this structure is similar to the traditional "if-then-else", but the "IF" function is substituted by the ternary operator "?".

#result =((@currency=='British pound') ? @numeric_value.ToString('C', #GetCulture('en-gb')) :

         (@currency=='Brazilian Real')? @numeric_value.ToString('C', #GetCulture('pt-br')) :

         (@currency=='American Dollar')? @numeric_value.ToString('C', #GetCulture('en-us')) :                                                          @numeric_value.ToString('C', #GetCulture('en-us')));

#result

 

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

 

In the formula above, the #GetCulture() variable contains the regional code that the ToString function will use to transform the value of the Numeric Value attribute into a text with currency format (in other words, of the country whose currency will be used). The regional codes supported can be found in the following page: https://msdn.microsoft.com/en-us/goglobal/bb896001.aspx.

In this example, if the "American Dollar" option is selected in the Currency field and the value entered in the Numeric Value field is 1059,76, the formula of the Monetary Value field will return the "$1.059,76" text. Note that, in addition to entering the currency symbol ($), the separators of thousands (period) and cents (comma) are adapted to the (en-us) regional currency format informed in the parameter.

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, the "en-us" format).