Difference between revisions of "Text Concatenation Operator: &"

m (adding doc status category)
Line 6: Line 6:
  
 
evaluates A and B, coerces their element values to text, and concatenates the result.
 
evaluates A and B, coerces their element values to text, and concatenates the result.
 +
 +
When concatenating numeric values to text, a number format is required to determine how the numbers should be rendered as text, what precision should be depicted, etc.  The number format for the variable or [[User-Defined Function]] being evaluated is used.
  
 
= Examples =
 
= Examples =
Line 12: Line 14:
 
:5 & 2 → '52'
 
:5 & 2 → '52'
 
:['a','b'] & 'c' → ['ac','bc']
 
:['a','b'] & 'c' → ['ac','bc']
 +
 +
= Tips and Tricks =
 +
 +
The need to coerce numbers to text using a particular format often arises.  Instead of having to rely on setting the number format for the variable that you are defining for this purpose, a common technique is to create a [[User-Defined Function]] to perform the particular coercion of interest.  Then in your definition, you just call that function.
 +
 +
For example, create the User-defined function:
 +
 +
Function ToCurrencyText(x) := "" & x
 +
 +
Then while viewing the object window for ToCurrencyText, select '''Number Format...''' ''(Ctrl+B)'' and set to Fixed Point, Showing Currency, two digits, and thousand separators. 
 +
 +
With that function defined, elsewhere you can use the function to convert numeric values to currency text:
 +
 +
ToCurrencyText(Revenue)
 +
 +
= See Also =
 +
 +
* [[JoinText]]

Revision as of 21:18, 2 August 2011


Then syntax

A & B

evaluates A and B, coerces their element values to text, and concatenates the result.

When concatenating numeric values to text, a number format is required to determine how the numbers should be rendered as text, what precision should be depicted, etc. The number format for the variable or User-Defined Function being evaluated is used.

Examples

'Hello' & ' ' & 'world' → 'Hello world'
5 & 2 → '52'
['a','b'] & 'c' → ['ac','bc']

Tips and Tricks

The need to coerce numbers to text using a particular format often arises. Instead of having to rely on setting the number format for the variable that you are defining for this purpose, a common technique is to create a User-Defined Function to perform the particular coercion of interest. Then in your definition, you just call that function.

For example, create the User-defined function:

Function ToCurrencyText(x) := "" & x

Then while viewing the object window for ToCurrencyText, select Number Format... (Ctrl+B) and set to Fixed Point, Showing Currency, two digits, and thousand separators.

With that function defined, elsewhere you can use the function to convert numeric values to currency text:

ToCurrencyText(Revenue)

See Also

Comments


You are not allowed to post comments.