Difference between revisions of "Converting Numbers to Text"

 
m
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Concepts]]
+
[[Category:Styles and options]]
  
To convert a number to a text value, you can use string concatenation:
+
There are two ways to convert a number or date to a text value: One is to use the [[NumberToText]] function. It lets you specify the format explicitly as a parameter.  See [[NumberToText]] for details. The other, described here, uses the [[Text Concatenation Operator: &]] to concatenate the number with a text --- e.g., <code>"" & x</code>
<code>
 
"" & x
 
</code>
 
  
;;Examples:
+
==Examples==
<code>
 
:"" & (2^10) &rarr; '1024'
 
:"" & Pi &rarr; '3.142'
 
:"" & [[Exp]](10) &rarr; '22.03K'
 
</code>
 
  
The coercion uses the number format for the object that contains the expression being evaluated. So, to use a different number format for the coercion, you must set the number format for that object.
+
:<code>"" & (2^10) &rarr; '1024'</code>
 +
:<code>"" & Pi &rarr; '3.142'</code>
 +
:<code>"" & Exp(10) &rarr; '22.03K'</code>
  
To avoid confusion between the format for displaying results and the format used for number-to-text conversion, a better way to perform the conversion is to define a [[User-Defined Function]] for the specific number format you are interested inFor example:
+
Text concatenation automatically converts ("coerces) any number to text. It uses the [[Number format]] set for the variable whose definition contains the expression.  So, the result may vary depending on the Number format specified. To use a different number format for the coercion, you must set the number format for that objectWhen you set the number format while viewing a table for the variable, be sure you select the whole table, not just selected cells -- because cell-level formats don't affect the coercion to text.
  
Function NumberToYYYY_MMM_DD(x) := "" & x
+
== See Also ==
{ Set its number format to Date &rarr; Custom &rarr; YYYY-MMM-DD }
 
  
Function NumberToDollars(x) := "" & x
+
* [[Text Concatenation Operator: &|string concatenation]]
{ Set its number format to Fixed Point, currency, two digits, show trailing zeroes }
+
* [[Function_Parameter_Qualifiers#Coerce_.3Ctype.3E|Coerce function parameter qualifier]]
 
+
* [[Numbers]]
With these, you can now accomplish the desired coercions using these functions. The number format for the function determines the conversion, rather than the number format for your variable.
+
* [[Numbers and text]]
 
+
* [[NumberToText]]
= See Also =
 
 
 
* [[Function Parameter Qualifiers#Coerce|Coerce Function Parameter Qualifier]]
 
 
* [[ParseNumber]]
 
* [[ParseNumber]]
 
* [[ParseDate]]
 
* [[ParseDate]]
 
* [[Number Format Dialog]]
 
* [[Number Format Dialog]]
 
* [[User-Defined Functions]]
 
* [[User-Defined Functions]]
 +
* [[Math functions]]
 +
* [[Text functions]]
 +
* [[Multiple formats in one table]]

Latest revision as of 23:26, 18 July 2018


There are two ways to convert a number or date to a text value: One is to use the NumberToText function. It lets you specify the format explicitly as a parameter. See NumberToText for details. The other, described here, uses the Text Concatenation Operator: & to concatenate the number with a text --- e.g., "" & x.

Examples

"" & (2^10) → '1024'
"" & Pi → '3.142'
"" & Exp(10) → '22.03K'

Text concatenation automatically converts ("coerces) any number to text. It uses the Number format set for the variable whose definition contains the expression. So, the result may vary depending on the Number format specified. To use a different number format for the coercion, you must set the number format for that object. When you set the number format while viewing a table for the variable, be sure you select the whole table, not just selected cells -- because cell-level formats don't affect the coercion to text.

See Also

Comments


You are not allowed to post comments.