Difference between revisions of "Text Concatenation Operator: &"

(Mention of Formatted Text Literals.)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
[[category:Operators]]
 
[[category:Operators]]
 
[[Category:Doc Status C]] <!-- For Lumina use, do not change -->
 
[[Category:Doc Status C]] <!-- For Lumina use, do not change -->
 +
{{ReleaseBar}}
  
Then syntax
+
== A & B==
A & B
+
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.
+
=== Concatenating numeric values ===
 +
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. Only the number format for the whole variable is used -- if you have set cell-level formats (such as a different number format for a particular slice), this does not impact the behavior of the number-to-text coercion. If in doubt, you can use the [[NumberToText]] function to control the format of converted numbers before applying the & operator.
  
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.
+
{{Release|6.0||When you are concatenating numeric values, a ''much'' better option is to use a [[Formatted Text Literal]], which results in a more readable expression and you can easily control the number format.}}
  
= Examples =
+
== Examples ==
 +
:<code>'Hello' & ' ' & 'world' &rarr; 'Hello world'</code>
 +
:<code>5 & 2 &rarr; '52'</code>
 +
:<code>['a', 'b'] & 'c' &rarr; ['ac', 'bc']</code>
  
:'Hello' & ' ' & 'world' &rarr; 'Hello world'
+
== Details==
:5 & 2 &rarr; '52'
+
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, you can use the [[NumberToText]] function.
:['a','b'] & 'c' &rarr; ['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 =
 
  
 +
== See Also ==
 
* [[JoinText]]
 
* [[JoinText]]
 +
* [[Concat]]
 +
* [[ConcatRows]]
 +
* [[ConcatN]]
 +
* [[Set_Functions#Function_SetUnion|SetUnion]]
 +
* [[Aggregate]]
 +
* [[NumberToText]]
 +
* [[Text functions]]
 +
* [[Formatted Text Literals]]

Latest revision as of 20:42, 8 October 2021



Release:

4.6  •  5.0  •  5.1  •  5.2  •  5.3  •  5.4  •  6.0  •  6.1  •  6.2  •  6.3  •  6.4  •  6.5  •  6.6


A & B

Evaluates «A» and «B», coerces their element values to text, and concatenates the result.

Concatenating numeric values

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. Only the number format for the whole variable is used -- if you have set cell-level formats (such as a different number format for a particular slice), this does not impact the behavior of the number-to-text coercion. If in doubt, you can use the NumberToText function to control the format of converted numbers before applying the & operator.

When you are concatenating numeric values, a much better option is to use a Formatted Text Literal, which results in a more readable expression and you can easily control the number format.

Examples

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

Details

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, you can use the NumberToText function.

See Also

Comments


You are not allowed to post comments.